如何解决此错误:“ _ InternalLinkedHashMap<动态,动态>”不是'Map< String,dynamic>'类型的子类型 [英] How to fix this Error: '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'
问题描述
我尝试在Flutter应用中解析Firestore中的文档。
i try to parse a document from Firestore in my flutter app.
Firestoredocument:
Firestoredocument:
我创建了两个类来解析此文档。
I created two classes to parse this document.
类产品:
class Produkt{
String name;
int anzahl;
Produkt({
this.name,
this.anzahl,
});
factory Produkt.fromJson(Map<String, dynamic> parsedJson){
return Produkt(
name:parsedJson['Name'],
anzahl:parsedJson['Anzahl']
);
}
}
Class ProduktList:
Class ProduktList:
class ProduktList{
final List<Produkt> produkte;
ProduktList({
this.produkte,
});
factory ProduktList.fromJson(Map<String, dynamic> parsedJson){
var list = parsedJson["Produkte"] as List;
List<Produkt> produkte = list.map((i) => Produkt.fromJson(i)).toList();
return ProduktList(
produkte: produkte,
);
}
}
在我构建此文件时,会引发异常:
When i built this an Exception is thrown:
type '_InternalLinkedHashMap<dynamic, dynamic>' is not a subtype of type 'Map<String, dynamic>'
我认为错误发生在这里:
I think, that the error occurs here:
List<Produkt> produkte = list.map((i) => Produkt.fromJson(i)).toList();
U伙计们有个主意,我该如何解决这个问题?
谢谢您的回答!
U Guys have an Idea how i can fix this problem? Thank you for your answers!
推荐答案
您用于 parsedJson的Map对象
显然不是 Map< String,dynamic>
。
假设我们是能够将此对象转换为 Map< String,dynamic>
,请尝试:
Assuming that we are able to cast this object to Map<String, dynamic>
, try:
List<Produkt> produkte = list.map((i) => Produkt.fromJson(i.cast<String, dynamic>())).toList();
或:
List<Produkt> produkte = list.map((i) => Produkt.fromJson(Map<String, dynamic>.from(i))).toList();
在这里看看类似的问题:
如何转换从json创建的 _InternalLinkedHashMap?
take a look at a similar question here: How to cast an `_InternalLinkedHashMap` created from json?
如果没有帮助,请添加您如何使用 ProduktList.fromJson
的代码,因此我们会提供有关错误的更多信息。
If it does not help, please add the code of how do you use ProduktList.fromJson
, so we have more information about the error.
这篇关于如何解决此错误:“ _ InternalLinkedHashMap<动态,动态>”不是'Map< String,dynamic>'类型的子类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!