sách gpt4 ăn đã đi

dart - 如何修复此错误 : '_InternalLinkedHashMap' is not a subtype of type 'Map'

In lại 作者:IT王子 更新时间:2023-10-29 06:58:56 26 4
mua khóa gpt4 giày nike

我尝试在我的 flutter 应用程序中解析来自 Firestore 的文档。

Firestore 文档:

nhập mô tả hình ảnh ở đây

我创建了两个类来解析这个文档。

类产品:

class Produkt{
Tên chuỗi;
int anzahl;

Produkt({
this.name,
this.anzahl,
});

factory Produkt.fromJson(Map parsedJson){
return Produkt(
name:parsedJson['Name'],
anzahl:parsedJson['Anzahl']
);
}
}

类产品列表:

class ProduktList{
final List produkte;

ProduktList({
this.produkte,
});

factory ProduktList.fromJson(Map parsedJson){
var list = parsedJson["Produkte"] as List;
List produkte = list.map((i) => Produkt.fromJson(i)).toList();

return ProduktList(
produkte: produkte,
);
}
}

当我构建这个时抛出异常:

type '_InternalLinkedHashMap' is not a subtype of type 'Map'

我认为,错误发生在这里:

List produkte = list.map((i) => Produkt.fromJson(i)).toList();

你们知道我该如何解决这个问题吗?感谢您的回答!

câu trả lời hay nhất

您为 parsedJson 使用的 map 对象显然不是 Map .

假设我们能够将此对象转换为 Map , 尝试:

List produkte = list.map((i) => Produkt.fromJson(i.cast())).toList();

或:

List produkte = list.map((i) => Produkt.fromJson(Map.from(i))).toList();

在这里看看类似的问题: How to cast an `_InternalLinkedHashMap` created from json?

如果没有帮助,请添加您如何使用的代码ProduktList.fromJson , 所以我们有更多关于错误的信息。

关于dart - 如何修复此错误 : '_InternalLinkedHashMap' is not a subtype of type 'Map' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54167985/

26 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress