0

I have got the following Text( ... ) widget in my Flutter application

Text(
                                    data['location'],
                                    style: TextStyle(
                                        fontSize: 40, 
                                        letterSpacing: 2, 
                                        color: Colors.white
                                    )

However, it keeps showing me an error that the data['location'] cannot be accessed, as it can be null. I guess it has to do something with Dart's null safety checks

The flow of my code is something like this:

  1. The data object that I am accessing in the Text( ... ) widget is declared above like this:

     Object? data;
    
  2. Afterwards, when I pass data to this screen from another screen, I use the data object declared above and do the following assignment:

         data = ModalRoute.of(context)!.settings.arguments;
    
  3. And, finally, in the end, I access this data object in my Text( ... ) widget

What should I do to avoid this error?

John Doe
  • 87
  • 1
  • 9
  • You need to set `ModalRoute.of(context)` value in case of null first. ModalRoute.of(context) == null ? defaultData : ModalRoute.of(context).settings.arguments; Or if you are ok with `data` be null just go with `data = ModalRoute.of(context)?.settings.arguments;` – Simon Sot Jul 08 '21 at 13:31
  • @SimonSot the problem persists. It still gives the same error – John Doe Jul 08 '21 at 13:47

2 Answers2

1

1 - data is of type Object so you can't use [] with Object type because it does not overload [] operator

2 - let assume that argument is of type List you must cast it first to Map then access the data from it like this

 (data as Map?)?["location"]

3 - data is nullable Type all you need to do is to tell dart that I am sure that data is not null by bang operator

The finale code should like that:

Text(
  (data as Map?)?["location"]!, // <---
  style: TextStyle(
  fontSize: 40, 
  letterSpacing: 2, 
  color: Colors.white
)
Hosam Hasan
  • 564
  • 4
  • 11
  • Thanks @Hosam. Worked like a charm. I do have one question though: When I remove both the question marks in (data as Map?)?["location"]!, it still works fine. What could be the reason behind that? – John Doe Jul 08 '21 at 22:16
  • it's weird to cast nullable type to non-nullable type without error, I think that some null safe features still not complete yet. look here [cast_nullable_to_non_nullable](https://dart-lang.github.io/linter/lints/cast_nullable_to_non_nullable.html) – Hosam Hasan Jul 08 '21 at 22:43
0

arguments property has a return type of Object? which is the most generic type.

To make use of the actual data, you'll first need to use Typecast as operator.

var data = ModalRoute.of(context)?.settings.arguments as Map<String, dynamic>?;

Since data could be null

  1. you'll need to either set a default value like:
data ??= {'location': 'behind you'}; // Avoids null access.
  1. or if you are absolutely sure that the value won't be null:
data!['location']; // Will result in runtime exception if data is null.
happy-san
  • 810
  • 1
  • 7
  • 27