'Firestore type mismatch : The argument type 'List<Null>' can't be assigned to the parameter type 'List<JobPost>'
In a flutter project, I am trying to make a CollectionGroup query. But messed up with types. Here is my code :
Stream<Either<JobPostFailure, List<JobPost>>> watchAppliedJobPosts({
required String seamanId,
}) async* {
yield* _firestore
.collectionGroup(ConstStrings.applications)
.where(ConstStrings.seamanId, isEqualTo: seamanId)
.snapshots()
.map((querySnapshot) {
return right(querySnapshot.docs.map((docSnapshot) {
final jobPostDocRef = docSnapshot.reference;
jobPostDocRef.snapshots().map((doc) {
final jobPost = JobPostDto.fromFirestore(doc).toDomain();
return jobPost;
});
}).toList());
});
}
I expect to get a List<JobPost>, but getting following error at this line return right(querySnapshot.docs.map((docSnapshot) {...:
The argument type 'List<Null>' can't be assigned to the parameter type 'List<JobPost>'.
Though I am returning List<JobPost>, error says it is List<Null>. Where is the error? How to solve this?
Solution 1:[1]
You are missing a return keyword:
return /* <-- */ jobPostDocRef.snapshots().map((doc) {
That said, why are you complicating this function with an Either return type, when there clearly is no other option in your method? It always returns right, so you might as well just remove the Either from the return type.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | nvoigt |
