简体   繁体   English

如何从firebase实时数据库中获取int型数据

[英]How to get int type data from firebase real time database

I have tried to get the value of an int type from my database but it returns a null value i used a future to prevent that but still same problem it returns null我试图从我的数据库中获取 int 类型的值,但它返回一个 null 值

I tried calling the method inside the build function and also outside the build even went a step further to call it inside initState but no success whats the problem?我尝试在构建 function 内部调用该方法,在构建外部甚至更进一步在 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();
      }
    }
  );
}

I believe the issue is your ".value", the correct call as per documentation is:我相信问题是你的“.value”,根据文档正确的调用是:

return snapshot.data.val();

I believe you can optionally pass a data type into those brackets to attempt conversion to specific data type.我相信您可以选择将数据类型传递到这些括号中以尝试转换为特定数据类型。 Such as:如:

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM