![](/img/trans.png)
[英]Unhandled Exception: type 'String' is not a subtype of type 'List<dynamic>' in type cast
[英]Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'List<dynamic>?' in type cast
我从 firestore 获取数据并将数据存储为 List,由于来自 firestore 的数据是 map,我无法正确保存它,我收到了这个错误。
我的代码。
getData() async{
final dataDB = await FirebaseFirestore.instance.collection('TransactionData').doc(getUid()).get();
snapshot = dataDB;
final Map documentData = snapshot.data() as Map;
_cards = documentData as List?;
setState(() {
_data = _cards![0];
});
}
我来自 Firestore 的数据。
{21Aug5298Sav: [{categoryName: Education, uid: LNM6JQuoKCPIANTyQHMKWLIzl5m1, amount: 3,000.00, lastUpdatedDate: 25-08-2022, createdDate: 25-08-2022, toAccountId: , type: Credit, transfer: N, accountId: 21Aug5298Sav, auto: Y, accountName: null, transactionId: deb1621, categoryLogo: assets/images/Education.png, untagged: N, categoryBackgroundColor: 0xffFFA2C0, currency: INR, transactionDate: 25-08-2022}, {categoryName: Education, uid: LNM6JQuoKCPIANTyQHMKWLIzl5m1, amount: 300.00, lastUpdatedDate: 25-08-2022, createdDate: 25-08-2022, toAccountId: , type: Debit, transfer: N, accountId: 21Aug5298Sav, auto: Y, accountName: null, transactionId: deb5366, categoryLogo: assets/images/Education.png, untagged: N, categoryBackgroundColor: 0xffFFA2C0, currency: INR, transactionDate: 25-08-2022}, {categoryName: Travelling, uid: LNM6JQuoKCPIANTyQHMKWLIzl5m1, amount: 100.00, lastUpdatedDate: 24-08-2022, createdDate: 24-08-2022, toAccountId: , type: , transfer: N, accountId: 21Aug5298Sav, a<…>
我只希望列表包含从 Firestore 中检索到的所有数据。 有人可以帮我解决这个问题。
提前致谢。
在您尝试将 map 转换为列表并将其分配给 the_cards 变量的代码中。 尝试这个
final Map documentData = snapshot.data() as Map;
_cards = documentData.values.toList();
//Or
_cards = documentData.values.toList().first;
编辑
var allDocData = documentData.values.toList();
for(int i= 0; i < allDocData.length; i++)
{
_cards.add(allDocData[i]);
}
您不能将 Map 投射到列表中。 那是你的错误。 如果你检查你的数据,你会发现这不是一个列表,它是一个 Map。 如果你想要里面的列表,你需要通过密钥“21Aug5298Sav”访问
前任。
List _cards = documentData["21Aug5298Sav"]
尝试这个:
Map data = snapshot.data() as Map;
var firestoreResult = documentData.values.toList();
var finalResult = [];
for (var item in firestoreResult) {
for (var x in item) {
finalResult = [...finalResult, ...x];
}
}
并像这样使用它:
setState(() {
_data = finalResult![0];
});
我已经更改了我的 Firestore 数据的数据结构,并且它工作得很好。 感谢大家宝贵的时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.