【发布时间】:2020-10-11 02:13:10
【问题描述】:
我知道这可能看起来很明显,但我就是找不到办法做到这一点..
我正在尝试从 firebase 查询中获取 单个文档。当我说单个文档时,我的意思是不是流。
到目前为止,我的方法是:
MyClass getDocument(String myQueryString) {
return Firestore.instance.collection('myCollection').where("someField", isEqualTo: myQueryString) //This will almost certainly return only one document
.snapshots().listen(
(querySnapshot) => _myClassFromSnapshot(querySnapshot.documents[0])
);
}
但是,我收到错误 A value of type 'StreamSubscription<QuerySnapshot>' can't be returned from method 'getDocument' because it has a return type of 'MyClass'.
谢谢!
【问题讨论】:
-
你好,你试过了吗:databaseReference.collection('myCollection').getDocuments().then();
-
听起来不错,但它缺少查询部分。它会类似于: databaseReference.collection('myCollection').where("field", isEqualTo: myString).then() 吗?如何只获取第一个文档?
-
databaseReference.collection("myCollection").getDocuments().then((onValue) { onValue.documents.forEach((f) { f[0] })});
-
@AndresSilva 如果您只需要查询结果中的第一个文档,请尝试将查询限制为 1 个结果,请使用“.limit()”
标签: flutter dart google-cloud-firestore stream dart-stream