使用 Flutter 从 Firebase Firestore 查询中获取单个文档
Get a single document from Firebase Firestore query with Flutter
我知道这可能看起来很明显,但我就是找不到实现它的方法..
我正在尝试从 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;
}
},
);
}
2021 年更新:
var collection = FirebaseFirestore.instance.collection('myCollection');
var querySnapshot = await collection
.where('someField', isEqualTo: queryString)
.get();
for (var snapshot in querySnapshot.docs) {
Map<String, dynamic> data = snapshot.data();
}
您可以使用
从地图对象中检索数据
var fooValue = data['foo'];
2021 年 10 月更新
var coll = Firestore.instance.collection('yourcollection');
var qs = await coll.doc('yourdocument').get();
Map<String,dynamic> value = qs.data();
Map<String,dynamic> getField = jsonDecode(value['specific value in field']);
var valueInfield = getField['data'];
print($valueInfield);
我知道这可能看起来很明显,但我就是找不到实现它的方法..
我正在尝试从 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;
}
},
);
}
2021 年更新:
var collection = FirebaseFirestore.instance.collection('myCollection');
var querySnapshot = await collection
.where('someField', isEqualTo: queryString)
.get();
for (var snapshot in querySnapshot.docs) {
Map<String, dynamic> data = snapshot.data();
}
您可以使用
从地图对象中检索数据var fooValue = data['foo'];
2021 年 10 月更新
var coll = Firestore.instance.collection('yourcollection');
var qs = await coll.doc('yourdocument').get();
Map<String,dynamic> value = qs.data();
Map<String,dynamic> getField = jsonDecode(value['specific value in field']);
var valueInfield = getField['data'];
print($valueInfield);