Flutter Firebase 文件存储 - 将返回的列表转换为<dynamic>列表<CustomObject>



我正在从我的Firebase数据库中获取BaseObjects的列表。 这工作正常。

在这些对象上是一个自定义对象数组。

返回基本对象列表时,我还想将自定义对象转换为自定义对象的实际列表。

所以在火力数据库上:

BaseObject:
title: 'example'
custom_objects: // array
0
'name 0'
1
'name 1'

在我的应用程序中,我有一个基本对象和自定义对象的模型文件

所以当我检索基本对象列表时,它工作正常。 问题是如果我尝试在每个基本对象中获取自定义对象数组。

Stream<List<BaseObject>> get baseObjects {
return baseObjectCollection.snapshots().map(_baseObjectListFromSnapshot);
}
List<BaseObject> _baseObjectListFromSnapshot(QuerySnapshot snapshot) {
return snapshot.documents.map((doc) {
final baseObject = BaseObject(
title: doc.data['title'] ?? '',
customObjects: List<CustomObject>.from(doc.data['custom_objects']) ?? [],
);
return baseObject;
}).toList();
}

我得到的错误:

Exception:
type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'CustomObject'

更新:

I fixed it by doing:
customObjects: List<CustomObject>.from(doc.data["custom_objects"].map((item) {
return new CustomObject(
title: item["title"] ?? '',
);
},),) ?? [],

更新:

我通过以下方式修复了它:

customObjects: List<CustomObject>.from(doc.data["custom_objects"].map((item) {
return new CustomObject(
title: item["title"] ?? '',
);
},
),
) ?? [],

最新更新