I want to load the array from Firestore into a list in Flutter. I tried to use List.castFrom to convert Firestore array into List, but it does not work.
DocumentReference userDocRef = Firestore.instance.collection('users').document(id);
DocumentSnapshot userDocSnapshot = await userDocRef.get();
List groups = await List.castFrom(userDocSnapshot.data['groups']);
it shows an error: Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List'
This is the array in my Firestore:
What should I do?
You cannot directly turn a Map into a List. You would have to get to use forEach to split the map's key and values. Here is how you can get the Map's values and turn it into a List:
FirebaseDatabase.instance
.reference()
.child("users/$id")
.once()
.then((snapshot) {
if (snapshot.value != null) {
snapshot.value.forEach((k, v) {
myList.add(v);
});
} else {
print("value is null");
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.