扑扑火力地堡数据库获取用户ID,然后获取其他数据

时间:2018-09-08 13:03:53

标签: firebase firebase-realtime-database flutter

我使用userID如下创建数据库

enter image description here

当用户登录时,我可以获取userID,但是如果我使用userID从firebase数据库获取数据,则始终会出现以下错误。

请帮助解决此问题。

> NoSuchMethodError: The method '[]' was called on null. Receiver: null
> Tried calling: []("Email")

以下是我的代码:

      @override
      void initState() {
        super.initState();
        _loadCurrentUser();
        shop = Shop();
        databaseReference = database.reference().child("香港");
      }
      void _loadCurrentUser() {
        FirebaseAuth.instance.currentUser().then((FirebaseUser user) {
          setState(() {
            // call setState to rebuild the view
            this.currentUser = user;
          });
        });
      }
      String _email() {
        if (currentUser != null) {
          return currentUser.email;
        } else {
          return "no current user";
        }
      }
      String _userID() {
        if (currentUser != null) {
          debugPrint(currentUser.uid);
          databaseReference.child(currentUser.uid).once().then(_updateToDo);
          return currentUser.uid;
        } else {
          return "no current user";
        }
      }

      @override
      Widget build(BuildContext context) {
        void _signOut() async {
          try {
            await widget.auth.signOut();
            widget.onSignOut();
          } catch (e) {
            print(e);
          }
        }
        return new Scaffold(
          backgroundColor: Colors.blueGrey,
          appBar: new AppBar(
            backgroundColor: Colors.black,
            title: Text(_email()),

          ),
          body:
            createFireList(),
        );
      }

      Widget createFireList() {
        return Column(
          children: <Widget>[
            Text(_userID()),
            Text(test)

          ],
        );
      }

      _updateToDo(DataSnapshot map) {
        if (map.value != null) {
          var x = map.value["Email"];
          setState(() {
            test = x;
          });
        }
        else {
          debugPrint("Error");
        }
      }
    }

0 个答案:

没有答案