Home > Mobile >  The method '[]' can't be unconditionally invoked because the receiver can be 'nu
The method '[]' can't be unconditionally invoked because the receiver can be 'nu

Time:07-08

import 'package:flutter/material.dart';

class Home extends StatefulWidget {

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {

  Map data = {};

  @override
  Widget build(BuildContext context) {

    final data = ModalRoute.of(context)!.settings.arguments;
    print(data);

    return Scaffold(
      body: SafeArea(
          child: Column(
            children: <Widget>[
              TextButton.icon(
                  onPressed: () {
                    Navigator.pushNamed(context, '/location');
                  },
                  icon: Icon(Icons.edit_location),
                  label: Text('Edit Location'),
              ),
              SizedBox(height: 20.0),
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: <Widget>[
                  Text(
                    data['location'],
                    style: TextStyle(
                      fontSize: 20.0,
                      letterSpacing: 2.0,
                    ),
                  ),
                ],
              )
            ],
          )
      ),
    );
  }
}

I get the following error:

The method '[]' can't be unconditionally invoked because the receiver can be 'null'. Can anyone solve line 35 data['location']

CodePudding user response:

You need to add a cast using the as keyword to let Dart know that dealing with a Map.

Instead of:

final data = ModalRoute.of(context)!.settings.arguments;

Use:

final data = ModalRoute.of(context)!.settings.arguments as Map;

CodePudding user response:

Firstly, It is better to make data nullable while we are not sure if previous route will pass data or not.

 final data = ModalRoute.of(context)?.settings.arguments as Map?;

While reading a map, we might not value for certain keys. That's why it is showing this error. You can provide default value when ever data['location'] will get null like

data?['location']?? "",

Also, you can skip Text widget rendering on null case

if (data?['location'] != null)
  Text(
    data!['location'],
    style: TextStyle(
      fontSize: 20.0,
      letterSpacing: 2.0,
    ),
  ),

I am using bang! operator after checking null, be aware of using !,

class _HomeState extends State<Home> {
  @override
  Widget build(BuildContext context) {
    final data = ModalRoute.of(context)?.settings.arguments as Map?;

    return Scaffold(
      body: SafeArea(
          child: Column(
        children: <Widget>[
          Text(
            data?['location'] ?? "default value",
            style: TextStyle(
              fontSize: 20.0,
              letterSpacing: 2.0,
            ),
          ),
          SizedBox(height: 20.0),
          Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              if (data?['location'] != null)
                Text(
                  data!['location'],
                  style: TextStyle(
                    fontSize: 20.0,
                    letterSpacing: 2.0,
                  ),
                ),
            ],
          )
        ],
      )),
    );
  }
}
  • Related