有没有一种使用Firestorage的方法,可以在没有Streambuilder / widget的情况下将集合转换为地图列表?

Edu*_*chi 2 dart flutter google-cloud-firestore

有没有一种使用Firestorage的方法,可以在没有Streambuilder / widget的情况下将集合转换为地图列表?

示例:我有一个包含文档的“ exampleData”集合。我需要使用集合的数据从中创建“地图列表”。数据示例:

List<Map> exampleData = [map1,map2,map3]
Run Code Online (Sandbox Code Playgroud)

该列表中的元素具有如下数据:

map1 = {
"active":"true"
"age" = "2"
"editing" = "false"
"photo_url" = "https://test.com"
"score: 0"
}
Run Code Online (Sandbox Code Playgroud)

cre*_*not 6

假设您有一个名为的变量listOfMaps,其类型为List<Map>

List<Map> listOfMaps;
Run Code Online (Sandbox Code Playgroud)

您可以为该对象提供集合的实时更新,如下所示:

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);
Run Code Online (Sandbox Code Playgroud)

我们可以使用snapshots() Stream<List<QuerySnapshot>>获得Stream包含的所有 文件对象(DocumentSnapshot。为此,我们可以使用map函数将转换List<QuerySnapshot>List<Map

data物业的每一个DocumentSnapshotList<DocumentSnapshot>持有List<Map>从数据库各个领域

// alternatively the Stream can be used e.g. in a StreamBuilder
StreamBuilder(
  stream: mappedStream,
  builder: ...
);
Run Code Online (Sandbox Code Playgroud)

总结一下:

  • 转换之前,我们可以List<DocumentSnapshot>snapshots() Stream

  • 之后,我们已经完成转换的成List<Map>通过使用data该财产DocumentSnapshots

  • mappedStream既可以在听了例如指定数据在我们的应用程序中的对象或像一个使用StreamBuilder