【问题标题】:How to get int type data from firebase real time database如何从firebase实时数据库中获取int类型数据
【发布时间】:2020-12-30 22:20:31
【问题描述】:

我试图从我的数据库中获取一个 int 类型的值,但它返回了一个空值

我尝试在构建函数内部和构建外部调用该方法,甚至更进一步在 initState 内部调用它,但没有成功是什么问题?

Widget build(BuildContext context){...}
int getSnapShotDataAsInt(String child){
  FutureBuilder(
    future: databaseReference
      .child("users")
      .child(_firebaseUser.uid)
      .child(child)
      .once(),
    builder: (BuildContext context, AsyncSnapshot snapshot) {
      if (snapshot.hasData && snapshot.data != null) {
        return snapshot.data.value;
      } else {
        return CircularProgressIndicator();
      }
    }
  );
}

【问题讨论】:

  • 诸如“但没有成功”和“它对我不起作用”之类的陈述非常难以帮助。请记住,Stack Overflow 是一个出了名的低效交互式调试器,我们需要确切地知道哪一行没有按照您的预期执行,以及它做了什么。那么如果在return snapshot.data.value 上设置断点,snapshot.data.value 会显示什么值?您能否print 该值,并在您的问题中向我们展示更新后的代码及其输出?
  • /users/$uid 看到 JSON 也很好(作为文本,请不要截图)。您可以通过单击Firebase Database console 上溢出菜单 (⠇) 中的“导出 JSON”链接来获取此信息。

标签: firebase flutter firebase-realtime-database


【解决方案1】:

我认为问题在于您的“.value”,根据文档的正确调用是:

return snapshot.data.val();

我相信您可以选择将数据类型传递到这些括号中以尝试转换为特定数据类型。 如:

return snapshot.data.val(String); (etc)

https://firebase.google.com/docs/reference/node/firebase.database.DataSnapshot#val

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 1970-01-01
    • 1970-01-01
    • 2021-04-08
    相关资源
    最近更新 更多