Flutter Fire Type cast AsyncSnapshot<QuerySnapShot<Map<String, dynamic>>> to List<dynamic>?

Issue

Cloud Firestore Database and Flutter.

My Implementation

AsyncSnapshot<QuerySnapshot<Map<String, dynamic>>> snapshot

After snapshot has data

if (snapshot.hasData) {
    final List<dynamic> _products = snapshot.data!docs[0].data() as List<dynamic>
}

Value of the expression snapshot.data.docs[0].data() using debugging I found it not null.
It is of the type _InternalLinkedHashMap

Solution

If the snapshot.data.docs[0].data() shows up as a _InternalLinkedHashMap it is a Map and not a List, so you can’t cast it to a list.

It sounds like you want:

snapshot.data.docs[0].data() as Map<String, dynamic>

If you get another error on that case, I recommend searching for the error message – as most of the common conversions have been covered before.

Answered By – Frank van Puffelen

Answer Checked By – David Goodson (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *