Flutter - The argument type 'Object' can't be assigned to the parameter type 'Map<String, dynamic>'
550
You probably need to explicitly cast doc.data()!
to Map<String, dynamic>
. Like this:
return AdminDto.fromJson(doc.data()! as Map<String, dynamic>).copyWith(id: doc.id);
Or correctly define the generic type of DocumentSnapshot
, like this:
factory AdminDto.fromFirestore(DocumentSnapshot<Map<String, dynamic>> doc) {
return AdminDto.fromJson(doc.data()!).copyWith(id: doc.id);
}
Author by
Divya Thampi
Updated on December 29, 2022Comments
-
Divya Thampi over 1 year
So I've been working on this project, and everything worked fine, until, I migrated the packages to null safety and encountered this error. Here's the code:
factory AdminDto.fromJson(Map<String, dynamic> json) => _$AdminDtoFromJson(json); factory AdminDto.fromFirestore(DocumentSnapshot doc) { return AdminDto.fromJson(doc.data()!).copyWith(id: doc.id); } }
Can someone please help me with this? The problem occurs when it comes to doc.data() parameter. I can't quite put my head into it.
-
Divya Thampi about 3 yearsOh my god! Sir, you're a life saver! I spent almost 5 hours trying to solve this error. And I have one more question: does wrapping doc.data() to doc.get(doc.data()!) helps in this case? Because I tried this one and it doesn't return any error
-
puelo about 3 yearsI am no expert when it comes to firestore, but to me
get()
looks more like a direct accessor of fields indata
, whiledata()
returns the whole dataset. You probably want the latter. -
Divya Thampi about 3 yearsOkay, Thank you so much, Sir ❤️