【问题标题】:Return a list of objects from firestore only once, NOT a stream. In Flutter仅从 firestore 返回一个对象列表,而不是一个流。在颤振中
【发布时间】:2019-09-03 13:00:18
【问题描述】:

我可以在 firestore 中添加和更新数据,我还可以检索集合流并将其转换为对象列表,但我不能只检索一次集合并将其转换为对象列表。

// 从 Firestore 获取流

Stream<QuerySnapshot> getDataDateStream(String uid, int startDateTime, int endDateTime) {
    CollectionReference usersDataCollection = Firestore.instance.collection('users').document(uid).collection('data');

    Stream<QuerySnapshot> snapshots = dataCollection.where('dataDateTime', isGreaterThanOrEqualTo: startDateTime).where('dataDateTime', isLessThanOrEqualTo: endDateTime).snapshots();

    return snapshots;
  }

// 将 Stream 转换为列表

List<DataSavedModel> ListToday = List<DataSavedModel>();
  StreamSubscription<QuerySnapshot> dataSubToday;

 dataSubToday = db.getDataDateStream(appState.user.uid, startTimeToday, todayEndTime).listen((QuerySnapshot snapshot) {
      final List<DataSavedModel> ModelListToday = snapshot.documents.map((documentSnapshot) => DataSavedModel.fromMap(documentSnapshot.data)).toList();
      setState(() {
        this.ListToday = ModelListToday;
      });
    });

这可行,但我不想返回流,因为我只想检索一次数据以迭代列表并对其执行操作。

【问题讨论】:

    标签: dart flutter google-cloud-firestore


    【解决方案1】:

    使用 getDocuments() 在 Query 或 CollectionReference 上查询文档一次。

    QuerySnapshot querySnapshot = await Firestore.instance.collection("collection").getDocuments();
    var list = querySnapshot.documents;
    

    【讨论】:

    • 这仅提供了一个文档列表,我如何根据我的数据模型将其转换为对象列表。就像我对流所做的那样,我的问题中的第二个代码块。
    • 如果我没记错的话,您的现有代码中已经有一行代码用于处理 QuerySnapshot 类型对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-01-11
    • 2020-04-07
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 1970-01-01
    相关资源
    最近更新 更多