Reputation: 467
I'm getting this error from Firebase Firestore code is :
factory NoteDto.fromJson(Map<String, dynamic> json) =>
_$NoteDtoFromJson(json);
factory NoteDto.fromFirestore(DocumentSnapshot doc) {
return NoteDto.fromJson(doc.data()).copyWith(id: doc.id);
}
The first factory is from 'jsonSerializable' package that I'm directly generate the code. But here for in fromFirestore I passed 'DocumentSnapshot' as argument while returning the method from "fromJson(doc.data())" It throws this error.
I also try this and it works :
factory NoteDto.fromJson(Map<String, dynamic> json) =>
_$NoteDtoFromJson(json);
factory NoteDto.fromFirestore(QueryDocumentSnapshot<Map<String, dynamic>> doc) {
return NoteDto.fromJson(doc.data()).copyWith(id: doc.id);
}
but later when I was implementing the watchAll notes interface code is :
@override
Stream<Either<NoteFailure, KtList<Note>>> watchAll() async* {
final userDoc = await _firebaseFirestore.userDocument();
yield* userDoc.noteCollection.snapshots().map((snapshot) =>
right<NoteFailure, KtList<Note>>(snapshot.docs.map((doc) => NoteDto.fromFirestore(doc).toDomain()).toImmutableList()),
);
}
you will see the fromFirestore it throws this error :
The argument type 'DocumentQuerySnapshot<Object?>' cant be assign to the parameter type 'QueryDocumentSnapshot<Map<String, dynamic>'
and inside the watchAll method the "userDocument()" and "noteCollection" are extensions implemented outside code :
extension FirestoreX on FirebaseFirestore {
Future<DocumentReference> userDocument() async {
final userOption = await getIt<IAuthFacade>().getSignedInUser();
final user = userOption.getOrElse(() => throw NotAuthenticatedError());
return FirebaseFirestore.instance.collection('users').doc(user.id.getOrCrash());
}
}
extension DocumentReferenceX on DocumentReference {
CollectionReference get noteCollection => collection('notes');
}
I'm confused what to do when I do the code as I mentioned first before "Also trying other way and throws different error", It throws this error which is on the title. but when the other way that throws this different they're quiet opposite. Can you help me with this I know It's long but ... :)
Upvotes: 0
Views: 1509
Reputation: 144
Remove the “<Map<String, dynamic>>” part from method arguments.
Then change “doc.data()” in to “doc.data()! as Map<String, dynamic>”.
factory NoteDTO.fromFirestore(DocumentSnapshot doc) {
return NoteDTO.fromJson(doc.data()! as Map<String, dynamic>)
.copyWith(id: doc.id);
}
You can get an idea about how to migrate to a new version of Cloud Firestore from the documentation. Refer to up-to-date documentation whenever possible.
Documentation: https://firebase.flutter.dev/docs/firestore/usage/
Upvotes: 1
Reputation: 340
For the facotry fromFirestore, you need to explicitly type what the DocumentSnapshot
is returning to <Map<String, dynamic>>
(default is Object?). then you will need to add a null check to the doc.data() (!).
factory NoteDto.fromFirestore(DocumentSnapshot<Map<String, dynamic>> doc) {
return NoteDto.fromJson(doc.data()!).copyWith(id: doc.id);
}
Upvotes: 2