是否有使用Firestorage的方法将集合转换为地图列表,而不使用Streambuilder / widget?
示例:我有一个包含文档的集合“exampleData”。我需要使用集合的数据从中创建“地图列表”。数据示例:
List<Map> exampleData = [map1,map2,map3]
该列表中的元素包含如下数据:
map1 = {
"active":"true"
"age" = "2"
"editing" = "false"
"photo_url" = "https://test.com"
"score: 0"
}
答案 0 :(得分:6)
假设您有一个名为listOfMaps
的变量,其类型为List<Map>
。
List<Map> listOfMaps;
您可以为该对象提供集合的实时更新,如下所示:
final Stream<List<Map>> mappedStream = collectionReference.snapshots().map((QuerySnapshot snapshot) {
// returning a Map that contains all values marked in your screenshot
return snapshot.documents.map((DocumentSnapshot document) => document.data);
});
// assigning the data to the listOfMaps by listening to the Stream
mappedStream.listen((List<Map> data) => listOfMaps = data);
我们可以使用snapshots()
Stream<List<QuerySnapshot>>
获取包含所有 文档的Stream
对象(DocumentSnapshot
)。为此,我们可以使用map
函数将List<QuerySnapshot>
转换为List<Map
。
DocumentSnapshot
中每个List<DocumentSnapshot>
的{{3}}与数据库中的每个字段都包含 List<Map>
// alternatively the Stream can be used e.g. in a StreamBuilder
StreamBuilder(
stream: mappedStream,
builder: ...
);
总结:
在转化之前,我们可以从List<DocumentSnapshot>
snapshots()
获得 Stream
之后,我们已使用 {{1}的List<Map>
属性将其转换为 data
}}的
DocumentSnapshots
可以被收听,例如将数据分配给我们的应用程序中的对象或用于data
property