简体   繁体   English

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

[英]Null error when I want my firebase user id with currentUser()

Error 错误

NoSuchMethodError: The method 'currentUser' was called on null. NoSuchMethodError:方法“ currentUser”在null上被调用。

Receiver: null 接收者:null

Tried calling: currentUser() 尝试调用:currentUser()

I'm interesting in know where i need to using this code: 我很有趣,知道我需要在哪里使用此代码:

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

This code is from the home_page.dart (see below) 该代码来自home_page.dart(请参见下文)

I had also tried to using: 我也曾尝试使用:

widget.auth.current.... widget.auth.current ....

root_page root_page

In bottom of the file i had tried different approach. 在文件的底部,我尝试了不同的方法。 Instead of HomePage then _HomePageState(comments out) 而不是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.dart

This is the class I'm using to get current user from auth. 这是我用来从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 home_page.dart

I had tried different things in this file but get different errors. 我在此文件中尝试了不同的操作,但是得到了不同的错误。

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;
      });
    });
  }

_auth.currentUser() does not return the user ID, it returns a FirebaseUser object . _auth.currentUser()不返回用户ID, 它返回FirebaseUser对象

Change it like this : 像这样更改它:

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

Yet, I would recommend to monitor the onAuthStateChanged stream instead . 但是,我建议改为监视onAuthStateChanged流 This way you will be informed when the user logs in or logs out immediately. 这样,当用户登录或立即注销时,将通知您。

Check this article , it covers it in depth. 查看本文 ,它涵盖了它的深度。

You can use like this. 您可以这样使用。

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

暂无
暂无

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

相关问题 为什么firebase.auth().currentUser即使用户登录也返回NULL - Why does firebase.auth().currentUser return NULL even when the user is logged in Vue Firebase:currentUser为空 - Vue Firebase : currentUser is null 如果我在 firebase 身份验证中使用 facebook 登录,为什么我的 currentUser 始终为空? - why my currentUser is always null if I login using facebook in firebase authentication? Firebase CurrentUser为空,如何保持用户登录所有活动? - Firebase CurrentUser Null, how to keep user logged in all activities? 当我在 node.js cli 中重新执行脚本时,firebase.auth().currentUser 正在返回 null - firebase.auth().currentUser is returning null when i re-execute a script in node.js cli Flutter firebase 登录用户在登录后返回 NULL currentUser - Flutter firebase logged in user returns a NULL currentUser after sign in firebase.auth()。当刷新页面时,currentUser返回null - firebase.auth().currentUser returns null when page is refreshed 加载页面时 Firebase auth.currentUser 为空,当页面加载几毫秒后调用 authstatechange 时用户加载 - Firebase auth.currentUser is null when loading the page, user loaded when authstatechange called after some milli seconds of page load 当页面重新加载时,firebase.auth.currentUser 返回 null - When the page is reloaded, firebase.auth.currentUser returns null 我想基于Angular中当前登录的用户ID使用firebase作为我的后端进行查询 - I want to make a query based on the current logged in user id in Angular using firebase as my back-end
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM