[英]how can i read data from firebase (one time read ) using (.get)
how can i get data from document in firebase using the method.get..... i tried this but it didn't work and gived me an empty string.如何使用 method.get 从 firebase 中的文档中获取数据.....我尝试了这个,但它没有用,并给了我一个空字符串。 i already tried another method i found on firebase website:
我已经尝试过在 firebase 网站上找到的另一种方法:
class GetUserName extends StatelessWidget {
final String documentId;
GetUserName(this.documentId);
@override
Widget build(BuildContext context) {
CollectionReference users = FirebaseFirestore.instance.collection('users');
return FutureBuilder<DocumentSnapshot>(
future: users.doc(documentId).get(),
builder:
(BuildContext context, AsyncSnapshot<DocumentSnapshot> snapshot) {
if (snapshot.hasError) {
return Text("Something went wrong");
}
if (snapshot.hasData && !snapshot.data!.exists) {
return Text("Document does not exist");
}
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
return Text("Full Name: ${data['full_name']} ${data['last_name']}");
}
return Text("loading");
},
);
but it i just need the value to pass it as a parametre like that但我只需要将该值作为这样的参数传递
String username = "";
users.doc(Uid).get().then((value) {
username = value.get('Username').toString();
});
any help please?请问有什么帮助吗?
I checked your code and until last part it looks ok for me.我检查了你的代码,直到最后一部分对我来说看起来还不错。
I think some problem is here:我认为这里有一些问题:
if (snapshot.connectionState == ConnectionState.done) {
Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
return Text("Full Name: ${data['full_name']} ${data['last_name']}"
}
First of all - try to print your response:首先 - 尝试打印您的回复:
Map<String, dynamic> data = snapshot.data!.data() as Map<String, dynamic>;
print('data: $data');
Then you can see what exactly is coming from your request.然后你可以看到你的请求到底是什么。 If it is empty - maybe you didn't set up your project and\or firebase correctly.
如果它是空的 - 也许您没有正确设置您的项目和\或 firebase。 Or it can be just wrong variable naming you trying to receive.
或者它可能只是您尝试接收的错误变量命名。
But the most fun reason could be just that you have empty database with no data in it.但最有趣的原因可能是您有一个空数据库,其中没有数据。 Just try to fill it with needed data.
只需尝试用所需的数据填充它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.