I'm trying to get data from a Firebase listener & use it as a Model Object but I'm getting Unhandled Exceptions.
The goal is to get the data, make it a list, and display it on a page in my app.
Here is where I use my firebase listen to get the data:
final chatRef = ref.child("ChildInfo").child(key.toString());
chatRef.onValue.listen(
(event) async {
final data = event.snapshot.value as Map<Object?, Object?>;
final cleanData = Map<String, dynamic>.from(data);
final List<ABCModel> abc = cleanData.values
.map(
(raw) => ABCModel.fromJson(raw),
)
.toList();
But I'm getting this Unhandled Exception:
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>'
This is my fromJson:
factory ABCModel.fromJson(Map<String, dynamic> json) => ABCModel(
id: json['id'] == null ? null : json['id'] as String?,
name: json['name'] == null ? null : json['name'] as String?,
description:
json['description'] == null ? null : json['description'] as String?,
imageUrl: json['imageUrl'] == null ? null : json['imageUrl'] as String?,
date: json['date'] == null
? null
: Parser.parseDateTime(json['date']) ?? DateTime.now(),
blockedUsers: json['blockedUsers'] == null
? null
: (json['blockedUsers'] as List<dynamic>)
.cast<Map<String, dynamic>>()
.map((item) => BlockedUsers.fromJson(item))
.toList(),
);
Update:
I have recently tried getting my data as:
Map<String, dynamic> data =
jsonDecode(jsonEncode(event.snapshot.value));
But, when trying to convert this to a list of my model objects I am still getting [VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type 'String' is not a subtype of type 'Map<String, dynamic>' OR [VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'
I'm using version: firebase_database: ^9.0.0