仅从firestore返回一次对象列表,而不是流。在Flutter

问题描述 投票:0回答:1

我可以在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个回答
1
投票

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

QuerySnapshot querySnapshot = await Firestore.instance.collection("collection").getDocuments();
var list = querySnapshot.documents;
© www.soinside.com 2019 - 2024. All rights reserved.