当我希望使用currentUser()的Firebase用户ID时为Null错误

时间:2018-09-25 17:42:02

标签: firebase firebase-authentication flutter

错误

NoSuchMethodError:方法'currentUser'在null上调用。

收件人:空

尝试调用:currentUser()

我很有趣,知道我需要在哪里使用此代码:

String _uid = '';
      void initState() {
        super.initState();
        auth.currentUser().then((userId) {
          setState(() {
            _uid = userId;
          });
        });
      }

此代码来自home_page.dart(请参见下文)

我也曾尝试使用:

widget.auth.current ....

root_page

在文件的底部,我尝试了不同的方法。 而不是HomePage,然后是_HomePageState(注释掉了)

class RootPage extends StatefulWidget {

  RootPage({this.auth});
  final BaseAuth auth;

  @override
  State<StatefulWidget> createState() => new _RootPageState();

}

enum AuthStatus {
  notSignedIn,
  signedIn
}

class _RootPageState extends State<RootPage>{

  AuthStatus authStatus = AuthStatus.notSignedIn;

  @override
  void initState() {
    super.initState();
    widget.auth.currentUser().then((userId) {
      setState(() {
        authStatus = userId == null ? AuthStatus.notSignedIn : AuthStatus.signedIn;
      });
    });
  }

  void _signedIn(){
    setState(() {
      authStatus = AuthStatus.signedIn;
    });
  }

  void _signedOut() {
    setState(() {
      authStatus = AuthStatus.notSignedIn;
    });
  }

  @override
  Widget build(BuildContext context) {
    switch(authStatus) {
      case AuthStatus.notSignedIn:
        return new LoginPage(
          auth: widget.auth,
          onSignedIn: _signedIn,
        );
      case AuthStatus.signedIn:
        /*
        return new HomePageState(
          auth: widget.auth,
          onSignedOut: _signedOut,
        );
        */
        return new HomePage(
          auth: widget.auth,
          onSignedOut: _signedOut,
        );
    }
  }
}

auth.dart

这是我用来从auth获取当前用户的类。

abstract class BaseAuth{
  Future<String> signInWithEmailAndPassword(String email, String password);
  Future<String> createUserWithEmailAndPassword(String email, String password);
  Future<String> currentUser();
  Future<void> signOut();
}

class Auth implements BaseAuth{

  final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;

  Future<String> signInWithEmailAndPassword(String email, String password) async {
    FirebaseUser user = await _firebaseAuth.signInWithEmailAndPassword(email: email, password: password);
    return user.uid;
  }

  Future<String> createUserWithEmailAndPassword(String email, String password) async {
    FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(email: email, password: password);
    return user.uid;
  }

  Future<String> currentUser() async {
    FirebaseUser user = await _firebaseAuth.currentUser();
    return user.uid;
  }

  Future<void> signOut() async {
    return _firebaseAuth.signOut();
  }

}

home_page.dart

我在此文件中尝试了其他操作,但得到了不同的错误。

class HomePage extends StatefulWidget {

  HomePage({this.auth, this.onSignedOut});

  final BaseAuth auth;
  final VoidCallback onSignedOut;

  void _signOut() async {
    try {
      await auth.signOut();
      onSignedOut();
    } catch (e) {
      print(e);
    }
  }
  @override
  State<StatefulWidget> createState() => new _HomePageState();
}

class _HomePageState extends State<HomePage> {
/*
  _HomePageState({this.auth, this.onSignedOut});
  final BaseAuth auth;
  final VoidCallback onSignedOut;
  */
  //final BaseAuth auth;

  String _uid = '';

  @override
  void initState() {
    super.initState();
    auth.currentUser().then((userId) {
      setState(() {
        _uid = userId;
      });
    });
  }

2 个答案:

答案 0 :(得分:1)

_auth.currentUser()不返回用户ID it returns a FirebaseUser object

像这样更改它:

auth.currentUser().then((user) {
  if (user == null || user.isAnonymous) {
    // what will you do?
    return;
  }
  setState(() {
    _uid = user.uid;
  });
});

但是,我建议您monitor the onAuthStateChanged stream instead。这样,当用户登录或立即注销时,系统会通知您。

选中this article,它会对其进行深入的显示。

答案 1 :(得分:0)

您可以这样使用。

someMethod() async {
   FirebaseUser user = await FirebaseAuth.instance.currentUser();
   print(user.uid);
}