Flutterfire: message_codes should explicitly support Map<String, dynamic> to better support JSON (Firebase)

Created on 13 Oct 2019  Â·  3Comments  Â·  Source: FirebaseExtended/flutterfire

@kevmoo suggested i file an issue report here in regards to our discussion in this thread: https://github.com/dart-lang/json_serializable/issues/137

To summarize, Flutter's Firebase package returns documents from Firestore collections as _InternalLinkedHashMap, but Flutter's recommended json package json_serializable expects json to come in as a Map. This results in the following error:

I/flutter ( 6352): type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>' where
I/flutter ( 6352):   _InternalLinkedHashMap is from dart:collection
I/flutter ( 6352):   Map is from dart:core
I/flutter ( 6352):   String is from dart:core

Here is an example of some code which will trigger this:

_firestore = Firestore.instance;

return _firestore.collection('favorites').where('user_id', isEqualTo: uid).getDocuments()
                .then((querySnap) {
                    querySnap.documents.forEach((document) {
                        print(document.data);

                        Map<String, dynamic> json = document.data; //casts, but if you put breaklines through this its that _InternalLinkedHashMap<dynamic, dynamic> type
                        Album album = new Album.fromJson(json['favorite_albums'][0]); //Throws the type exception
                    });
                })
                .catchError((error) {
                    print(error);
                });

Thank you!

crowd cloud_firestore bug

Most helpful comment

FYI: this isn't just about pkg:json_serializable – Dart's JSON logic deals with Map values as Map<String, dynamic> – it's also what folks expect

All 3 comments

@jmrboosties

The issue at https://github.com/flutter/flutter/issues/17417 has been closed and moved here. Future collaboration on this issue will be done here.

FYI: this isn't just about pkg:json_serializable – Dart's JSON logic deals with Map values as Map<String, dynamic> – it's also what folks expect

Hey all, as part of our on-going work for #2582, this has been resolved in our Firebase Firestore rework (#2913) - which has now been merged into master. We'll look at publishing some prereleases in the next few days. Thank you

Was this page helpful?
0 / 5 - 0 ratings