简体   繁体   English

如何使用 (.get) 从 firebase 读取数据(一次读取)

[英]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.

相关问题 如何从 flutter firebase base 中的单个文档读取一次数据(不是实时的)? - How do I read data from a single document in flutter firebase base once (not in real time)? 如何使用 angular 从 firebase 获取服务器时间? - How can i get server time from firebase using angular? 如何使用 Flutter/Dart 从 Firebase 实时数据库中读取数据 - How to read data from Firebase Realtime database using Flutter/Dart 无法使用 html 从 firebase 读取数据 - unable to read data from firebase using html 如何从 firebase 读取更新后的值? - How can I read an updated value from firebase? 无法从 Firebase 读取数据 - Unable to read Data from the Firebase 我正在尝试从 FireBase 读取信息,但出现此错误。 如何解决? - I'm trying to read information from FireBase, but I get this error. How to fix it? 从 javascript firebase 数据库读取数据时,如何读取嵌套的多个引用? - How can we read multiple references nested while reading data from javascript firebase database? 无法通过发出 GET 请求从 Firebase 实时数据库读取数据。 始终返回 null - Can't read data from Firebase Realtime database by issuing a GET request. Always returns null 如何使用 window 服务自动获取 firebase 数据 - How can i get firebase data automatically using window service
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM