Why compiler assuming `json['x'] = int?`?
Because the []
operator on Map
returns a nullable by spec:
V? operator [](Object? key)
The value for the given
key
, ornull
ifkey
is not in the map.
https://api.dart.dev/stable/2.15.1/dart-core/Map/operator_get.html
So if you are asking for a key
that is not in your Map
you will get a null
value back and not an exception.
If you are 100% sure json['x']
will always work and want the application to crash in case this is not the case, you can use json['x']!
. Alternative, you need to provide default values or other type of handling in case these values is not in the map.