Reputation: 718
I'm trying to return this List as a Stream, but I'm getting the below errors:
1. The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.
2. A value of type 'Future<QuerySnapshot<Map<String, dynamic>>>' can't be assigned to a variable of type 'QuerySnapshot<Object?>'.
3. A value of type 'List<dynamic>?' can't be returned from the method 'userBookmarkedCourses' because it has a return type of 'Stream<List<dynamic>?>'.
Listing below is the code that is causing these errors:
Stream<List<dynamic>?> userBookmarkedCourses() {
print("User Id within User Provider: $userId");
QuerySnapshot userSnapshot;
try {
if (userId != null) {
userSnapshot = FirebaseFirestore.instance.collection('users').where('userID', isEqualTo: userId).get();
if (userSnapshot.docs.isNotEmpty) {
return coursesBookmarkedList = userSnapshot.docs[0].get('coursesBookmarked');
}
return coursesBookmarkedList = [];
}
} catch (error) {
coursesBookmarkedList = [];
}
}
I would also like to know how to declare this within the MultiProviders
and do I consume it using the Provider.of
I'm new to Streams. What is the cause of this error?
Upvotes: 0
Views: 105
Reputation: 1089
1- You have to return something in catch block. Because the method userBookmarkedCourses has a return type.
2- You have to set QuerySnapshot<Map<String, dynamic>> as userSnapshot's type. Additionally, you must put await before FirebaseFirestore.instance.collection('users').where('userID', isEqualTo: userId).get();
3- Return type of method is a Stream<List?>. But, you are returning List<dynamic>?. Either change method's returning type to List<dynamic>? or return a variable which is a Stream<List?>
Upvotes: 1