How to fix flutter firestore data.containsKey issue

Viewed 418

Please, after updating firestore, I had some changes in my app, but I have fixed all except this one. What are the changes to document.data().ContainsKey('address'). Thanks

if (document.data().containsKey('address')) {
  address =
      UserAddress.fromMap(document['address'] as Map<String, dynamic>);
}
1 Answers

You need to cast the snapshot data into a Map.

Map<String, dynamic> dataMap = snapshot.data!.data() as Map<String, dynamic>;

if(dataMap.containsKey('address')) { // <-- You need to use the dataMap
    // Your code 
}
Related