0
// ignore_for_file: prefer_const_constructors

import 'package:flutter/material.dart';
import 'package:world_time/pages/loading.dart';

class Home extends StatefulWidget {
  const Home({Key? key}) : super(key: key);

  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
  Map data = {};

  @override
  Widget build(BuildContext context) {
    final data = ModalRoute.of(context)?.settings.arguments;
    

    return Scaffold(
      body: SafeArea(
          child: Column(
        children: [
          TextButton.icon(
            onPressed: () {
              Navigator.pushNamed(context, '/choose_location');
            },
            label: Text("Edit location"),
            icon: Icon(Icons.edit_location_alt_sharp),
          ),
                                   // ERROR  
          Text(data['location']), // The method '[]' can't be unconditionally invoked because the 
                                       receiver can be 'null'.
                                       Try making the call conditional (using '?.') or adding a 
                                       null check to the target ('!')
        ],
      )),
    );
  }
}

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56

1 Answers1

0

It is possible to get null value from map key, you can use default value

Text(data['location']??"default value")

Or ignore building the widget while it is null.

if(data['location']!=null)Text(data['location'])

Check more about null-safet.

Md. Yeasin Sheikh
  • 54,221
  • 7
  • 29
  • 56