'Flutter realtime database 9.0.6 List<Object?>' is not a subtype of type 'Map<dynamic, dynamic>'
After update Firebase Realtime Database to 9.0.X I cant converet snapshot.value to Map<dynamic, dynamic> or Map<String, dynamic>
final FirebaseDatabase database = FirebaseDatabase.instance;
database.setPersistenceEnabled(true);
itemRef = database.reference().child('_child1').child(_remoteConfigService.getStringLang);
itemRef.once().then((event) {
final _data1 = Map<String, dynamic>.from(event.snapshot.value as dynamic).forEach((key, value) =>
_notes.add(Item.fromRTDB(value)));
});
My Model Class:
class Item {
String key;
String code;
String spn;
Item({
required this.key,
required this.code,
required this.spn});
factory Item.fromRTDB(Map<String, dynamic> data) {
return Item(
key: data['key'] ?? '01',
code: data['code'] ?? 'A drink',
spn: data['spn'] ?? 'Beer');
}
toJson() {
return {
"key": key,
"code": code,
"spn": spn,
};
}
}
In all options I get error:
List<Object?>' is not a subtype of type 'Map<dynamic, dynamic>'
Solution 1:[1]
Seems like the keys in the child are all numbers so it's already a List
Try this:
final _data1 = List.from(event.snapshot.value as dynamic).forEach((key, value) =>
_notes.add(Item.fromRTDB(value)));
Solution 2:[2]
Future<Null> getStudies() async {
_data.clear();
DatabaseReference ref = FirebaseDatabase.instance.ref("etudes");
Query query = ref.orderByChild("type").equalTo(1);
// Get the Stream
Stream<DatabaseEvent> stream = query.onValue;
// Subscribe to the stream!
stream.listen((DatabaseEvent event) {
print('Event Type: ${event.type}'); // DatabaseEventType.value;
print('Snapshot: ${event.snapshot}'); // DataSnapshot
if(event.snapshot.exists) {
Map<String, dynamic> data = jsonDecode(jsonEncode(event.snapshot.value)) as Map<String, dynamic>;
data.forEach((key, value) async {
_data.add(Etude.fromJson(value));
});
notifyListeners();
} else {
//print("snapshot does not exists");
_loading = false;
notifyListeners();
}
});
notifyListeners();
return null;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Josteve |
| Solution 2 | Ndiaga GUEYE |
