In this example below which unmaps the json from Firebase.
Everywhere where I get data as Map<String, dynamic>
- like orderId
- its ok.
But everywhere where I have map inside another map - like address
- it gives me a warning
"Avoid method calls or property accesses on a "dynamic" target."
Its not critical but I found on google that it provokes productivity penalties. Can someone share why it is so and how to avoid it
Order.fromMap(Map<String, dynamic> data) {
orderId = data['orderId'] as int;
address = data['location']['address'].toString();
}
CodePudding user response:
First of all, I think it is worth noting that the mentioned size and runtime penalties are generally not a concern, they are likely to be unnoticable.
the best fix you have for this is simply to explicitly cast your value:
address = (data['location'] as Map<String, dynamic>)['address'].toString();
I am not sure if the above will be enough or if you will have to also cast address
:
address = ((data['location'] as Map<String, dynamic>)['address'] as Object).toString()
The performance/size issue comes from the time it takes the application to figure out if data['location']
is a map. by adding as
, you are telling it to not check and to not worry.