fireship-io / flutter-firebase-quizapp-course

QuizApp Built with Flutter & Firebase
https://fireship.io/courses/flutter-firebase/
563 stars 242 forks source link

Error in db.dart for Collection.streamData #11

Open FireJuun opened 4 years ago

FireJuun commented 4 years ago

db.dart provides an excellent, abstract means to connect Firestore documents and collections to Flutter without all the boilerplate. However, I noticed a bug when working with streams in db.dart. When creating a stream collection, such as by calling: StreamProvider<List<Example>>.value(value: Global.exampleRef.streamData())

...inside of a MultiProvider widget, you will get the following error message:

An exception was thrown by _MapStrean<QuerySnapshot, List> listened by StreamProvider<List>, but no 'catchError was provided.

Exception: type 'MappedListIterable<DocumentSnapshot, Example>' is not a subtype of type 'List'

In db.dart, Line 47 is missing a .toList() at the end: Stream<List<T>> streamData() { return ref.snapshots().map((list) => list.documents.map((doc) => Global.models[T](doc.data) as T).toList()); }

maliii3 commented 3 years ago

Hey, I might have found the solution for your problem. I was facing the same problem as you did, while browsing in the depth of the StackOverflow, I found this Hope it helps after 11 months :)

FireJuun commented 3 years ago

Haha. Thanks. :)

Adding .toList() to the end of line 47 was the answer, by the way. I'm not sure if I had made that clear in my description of the issue. Technically, I probably should have just submitted a PR with the solution/update...

lucygliang commented 3 years ago

Thank you! I was running into the same issue and adding the toList() worked for me.

jeswinjessor commented 3 years ago

Hi, I was trying to fetch data from firestore using provider and this exception was thrown, have no idea what went wrong.

            ════════ Exception caught by provider ══════════

The following assertion was thrown: An exception was throw by _MapStream<QuerySnapshot, List

listened by StreamProvider, but no catchError was provided. Exception: type 'int' is not a subtype of type 'double'

ADDRESS CLASS Address.fromFirestore(Map<String, dynamic> firestore) : addressID = firestore['addressID'], streetNumber = firestore['streetNumber'], streetName = firestore['streetName'], city = firestore['city'], province = firestore['province'], customerContact = firestore['customerContact'], numberOfBedrooms = firestore['numberOfBedrooms'], numberOfFamilyRooms = firestore['numberOfFamilyRooms'], squareFeet = firestore['squareFeet'], customerName = firestore['customerName'], appointmentDate = DateFormat("yyyy-MM-dd").parse(firestore['appointmentDate']), nextAppointmentDate = DateFormat("yyyy-MM-dd").parse(firestore['nextAppointmentDate']), price = firestore['price'];

DATABASE SERVICE Stream<List

getAddress(){ return _db.collection('addressCollection').snapshots().map((snapshot) => snapshot.docs.map((document) => Address.fromFirestore(document.data())).toList()); } WIDGET CLASS @override Widget build(BuildContext context) { final address = Provider.of<List

(context, listen: false); } MAIN providers: [ ChangeNotifierProvider(create: (context) => AddressProvider(),), StreamProvider(create: (context) => firestoreService.getAddress()), ],

**I have used another provider in the same project for different collection and it worked fine, just the address is giving hard time

jrheisler commented 3 years ago

When you upgrade your firestore you'll wind up with a different set of errors with one related to the streamData. I can't figure it out.

Stream<List> streamData() { return ref.snapshots().map((list) => list.documents.map((doc) => Global.modelsT as T) ); }

gives: Expected a value of type 'Map<dynamic, dynamic>', but got one of type '() => Map<String, dynamic>'

FireJuun commented 3 years ago

When you upgrade your firestore you'll wind up with a different set of errors with one related to the streamData. I can't figure it out.

Stream streamData() { return ref.snapshots().map((list) => list.documents.map((doc) => Global.modelsT as T) ); }

gives: Expected a value of type 'Map<dynamic, dynamic>', but got one of type '() => Map<String, dynamic>'

You need to add a () somewhere in your code. The error is that it wants a map as its return value, but it is instead receiving a function that creates a map... Hence the () before => Map...

Assuming everything worked before the upgrade, you may want to consider changing doc.data to doc.data()

@jrheisler let me know if that was your issue.

jrheisler commented 3 years ago

Exactly, but I changed it on the receiving end in the model to data(). Thank you!!!

realrk95 commented 3 years ago

I added my problem on SO. Putting the v.data() solved the error of Map subtypes, but now I get the stream returned as null. I don't understand what is going wrong here. I've printed the user id too and it shows correctly in the switchmap.

https://stackoverflow.com/questions/66092838/firestore-document-stream-of-user-data

Any help? @FireJuun @jrheisler

LongKhoa0706 commented 3 years ago

When you upgrade your firestore you'll wind up with a different set of errors with one related to the streamData. I can't figure it out. Stream streamData() { return ref.snapshots().map((list) => list.documents.map((doc) => Global.modelsT as T) ); } gives: Expected a value of type 'Map<dynamic, dynamic>', but got one of type '() => Map<String, dynamic>'

You need to add a () somewhere in your code. The error is that it wants a map as its return value, but it is instead receiving a function that creates a map... Hence the () before => Map...

Assuming everything worked before the upgrade, you may want to consider changing doc.data to doc.data()

@jrheisler let me know if that was your issue.

it working <3!

jrheisler commented 3 years ago

@LongKhoa0706 that was exactly how I fixed the issue on my side.

yussifm commented 3 years ago

======== Exception caught by provider ============================================================== The following assertion was thrown: An exception was throw by _MapStream<QuerySnapshot<Map<String, dynamic>>, List> listened by

StreamProvider<List>, but no catchError was provided.

Exception: Bad state: field does not exist within the DocumentSnapshotPlatform

====================================================================================================

igbokwenu commented 1 year ago

======== Exception caught by provider ============================================================== The following assertion was thrown: An exception was throw by _MapStream<QuerySnapshot<Map<String, dynamic>>, List> listened by

StreamProvider, but no catchError was provided.

Exception: Bad state: field does not exist within the DocumentSnapshotPlatform

====================================================================================================

I am having this exact error. Were you able to find a fix?