I am getting this error The argument type 'Object?' can't be assigned to the parameter type 'Map<String, dynamic>'.
. I have tried all the answers suggested on stake overflow and other pages but I am still getting the same error.
These are some of the solutions I have tried. Apparently it is supposed to be data()
and not data
.
class Record {
final String buyer_name;
// final int total_quantity;
final String seller_location;
final DocumentReference reference;
Record.fromMap(Map<String, dynamic> map, {required this.reference})
: assert(map['buyer_name'] != null),
// assert(map()['total_quantity'] != null),
assert(map['seller_location'] != null),
buyer_name = map['buyer_name'],
// total_quantity = map()['total_quantity'],
seller_location = map['seller_location'];
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data(), reference: snapshot.reference);
@override
String toString() => "Record<$buyer_name:$seller_location>";
}
I have also added ()
to map
but I am still getting the same error.
CodePudding user response:
The QuerySnapshot
and DocumentSnapshot
types are genericized nowadays, meaning you need to be explicit about the data type you expect to get out of it.
Ideally you'd declare the query you use to read this data with a type as shown in the documentation on migrating to cloud_firestore 2. With that you'd then get this in the code you shared:
Record.fromSnapshot(DocumentSnapshot<Map<String, dynamic> snapshot)
: this.fromMap(snapshot.data(), reference: snapshot.reference);
But as said: you'll need to add the type to more code than just what you shared in the question, so be sure to study and follow the doc I linked.
Alternatively, you can also hard-cast the snapshot.data()
to the type you expect:
Record.fromSnapshot(DocumentSnapshot snapshot)
: this.fromMap(snapshot.data() as Map<String, dynamic>, reference: snapshot.reference);