我知道这看起来似乎很明显,但我只是找不到解决方法。
我正在尝试从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'.
谢谢!
多谢您的回覆,
通过这样做,我终于使它起作用了:
Future<MyClass> getDocument(String myQueryString) {
return Firestore.instance.collection('myCollection')
.where("someField", isEqualTo: myQueryString)
.limit(1)
.getDocuments()
.then((value) {
if(value.documents.length > 0){
return _myClassFromSnapshot(value.documents[0]);
} else {
return null;
}
},
);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句