【发布时间】:2020-07-01 02:48:32
【问题描述】:
我正在尝试创建一个 List[Map[String, dynamic]] 并将其保存到 firestore 并检索它。问题出在列表上,它给了我:type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>'
我真正想做的就是将下面的属性保存到“类”列表中,并检索它们的索引以显示在屏幕上。
类
class Cards {
final List<String> question;
final List<String> answer;
final String title;
final String uid;
final List<Map<String, dynamic>> classes;
Cards({ this.question, this.answer, this.uid, this.indexTitle, this.classes });
}
首页
List<Map<String, dynamic>> listMap = [];
listMap.add({
"title": title,
"question": [],
"answer": []
});
DatabaseService(uid: userId.uid).settingUserData(listMap);
数据库服务
// Set data to firestore db
Future settingUserData(List<Map<String, dynamic>> listMap) async {
return await _collref.document(uid).setData({
"classes": listMap
});
}
Cards _indexCardFromSnapshot(DocumentSnapshot snapshot) {
return Cards(
uid: uid,
classes: snapshot.data["classes"], // type 'List<dynamic>' is not a subtype of type 'List<Map<String, dynamic>>'
indexTitle: snapshot.data["indexTitle"],
question: snapshot.data["question"],
answer: snapshot.data["answer"],
);
}
【问题讨论】:
-
您在检索数据时遇到此错误吗?
-
是的,很抱歉。尝试将其作为快照接收时出现此错误@AliHussam
-
你确定这个错误是在'_indexCardFromSnapshot'函数中你试图返回卡的地方吗?
-
是的,如果我从“_indexCardFromSnapshot”中排除“类”,一切正常,但是当我包含它时,它就会抛出它。
标签: firebase flutter dart google-cloud-firestore