无法解决这个问题。我将以下文档设置为firestore。我需要将“出勤”字段转换为列表。
Map<String, dynamic> data = {
"name": "memberName",
"attendance": {
"0": {"morning": false, "noon": true, "night": true},
"1": {"morning": false, "noon": true, "night": true}
},
"deposits": {
"0": {"date": "date", "amount": 100},
"1": {"date": "date", "amount": 100}
}
};
使用StreamProvider。流没有问题。使用toList()
已提取文档。但是模型失败了。遇到异常:[类型'_InternalLinkedHashMap'不是'地图'类型的子类型]型号类别:
class Member {
String id;
String name;
List<Attendance> attendances;
List<Deposit> deposits;
Member({this.id, this.name, this.attendances, this.deposits});
Member.fromSnapshot(DocumentSnapshot doc) {
Map<String, dynamic> json = doc.data;
id = doc.documentID;
name = json['name'];
if (json['attendance'] != null) {
Map<String, dynamic> atdmap = json['attendance'];
attendances = new List<Attendance>();
atdmap.forEach((k, v) {
attendances.add(new Attendance.fromJson(v));
});
}
if (json['deposits'] != null) {
Map<String, dynamic> dmap = json['deposits'];
deposits = new List<Deposit>();
dmap.forEach((k, v) {
deposits.add(new Deposit.fromJson(v));
});
}
}
}
class Attendance {
bool morning;
bool noon;
bool night;
Attendance({this.morning, this.noon, this.night});
Attendance.fromJson(Map<String, dynamic> json) {
morning = json['morning'];
noon = json['noon'];
night = json['night'];
}
}
class Deposit {
String date;
int amount;
Deposit({this.date, this.amount});
Deposit.fromJson(Map<String, dynamic> json) {
date = json['date'];
amount = json['amount'];
}
}
哪里出错了。需要一点帮助。谢谢。
尝试显式转换
Map<String, dynamic> json = doc.data as Map<String, dynamic>;