当我希望firebase用户id为currentUser()时出现Null错误

当我希望firebase用户id为currentUser()时出现Null错误,firebase,firebase-authentication,flutter,Firebase,Firebase Authentication,Flutter,错误 NoSuchMethodError:对null调用了方法“currentUser” 收件人:空 尝试调用:currentUser() 我很想知道我需要在哪里使用这些代码: String _uid = ''; void initState() { super.initState(); auth.currentUser().then((userId) { setState(() { _uid = userI

错误

NoSuchMethodError:对null调用了方法“currentUser”

收件人:空

尝试调用:currentUser()

我很想知道我需要在哪里使用这些代码:

String _uid = '';
      void initState() {
        super.initState();
        auth.currentUser().then((userId) {
          setState(() {
            _uid = userId;
          });
        });
      }
此代码来自主页.dart(见下文)

我还尝试使用:

widget.auth.current

根目录页面

在文件的底部,我尝试了不同的方法。 而不是主页,然后是主页状态(注释)

class RootPage扩展StatefulWidget{
RootPage({this.auth});
最终BaseAuth-auth;
@凌驾
State createState()=>new_RootPageState();
}
枚举身份验证状态{
没有签名,
签字人
}
类_RootPageState扩展了状态{
AuthStatus AuthStatus=AuthStatus.notSignedIn;
@凌驾
void initState(){
super.initState();
widget.auth.currentUser().then((userId){
设置状态(){
authStatus=userId==null?authStatus.notSignedIn:authStatus.signedIn;
});
});
}
void _signedIn(){
设置状态(){
authStatus=authStatus.signedIn;
});
}
void _signedOut(){
设置状态(){
authStatus=authStatus.notSignedIn;
});
}
@凌驾
小部件构建(构建上下文){
交换机(身份验证状态){
案例AuthStatus.notSignedIn:
返回新登录页(
auth:widget.auth,
onSignedIn:_signedIn,
);
案例AuthStatus.signedIn:
/*
返回新的HomePageState(
auth:widget.auth,
onSignedOut:_signedOut,
);
*/
返回新主页(
auth:widget.auth,
onSignedOut:_signedOut,
);
}
}
}
验证省道

这是我用来从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();
  }

}
抽象类BaseAuth{
未来使用email和password登录(字符串电子邮件、字符串密码);
未来createUserWithEmailAndPassword(字符串电子邮件、字符串密码);
Future currentUser();
未来签出();
}
类Auth实现BaseAuth{
final FirebaseAuth _FirebaseAuth=FirebaseAuth.instance;
带email和密码(字符串电子邮件、字符串密码)的未来登录异步{
FirebaseUser user=wait _firebaseAuth.Signin with email and password(电子邮件:电子邮件,密码:密码);
返回user.uid;
}
Future createUserWithEmailAndPassword(字符串电子邮件、字符串密码)异步{
FirebaseUser user=wait_firebaseAuth.createUserWithEmailAndPassword(电子邮件:电子邮件,密码:密码);
返回user.uid;
}
Future currentUser()异步{
FirebaseUser=等待_firebaseAuth.currentUser();
返回user.uid;
}
Future signOut()异步{
返回_firebaseAuth.signOut();
}
}
主页.省道

我在这个文件中尝试了不同的方法,但得到了不同的错误

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;
      });
    });
  }
类主页扩展StatefulWidget{
主页({this.auth,this.onSignedOut});
最终BaseAuth-auth;
最终作废回拨至已签出;
void\u signOut()异步{
试一试{
等待auth.signOut();
onSignedOut();
}捕获(e){
印刷品(e);
}
}
@凌驾
State createState()=>new_HomePageState();
}
类_HomePageState扩展状态{
/*
_HomePageState({this.auth,this.onSignedOut});
最终BaseAuth-auth;
最终作废回拨至已签出;
*/
//最终BaseAuth-auth;
字符串_uid='';
@凌驾
void initState(){
super.initState();
auth.currentUser().then((用户ID){
设置状态(){
_uid=用户id;
});
});
}
您可以这样使用

someMethod() async {
   FirebaseUser user = await FirebaseAuth.instance.currentUser();
   print(user.uid);
}
\u auth.currentUser()
不返回用户ID

更改如下:

auth.currentUser().then((user) {
  if (user == null || user.isAnonymous) {
    // what will you do?
    return;
  }
  setState(() {
    _uid = user.uid;
  });
});
但是,我建议您这样做。这样,当用户立即登录或注销时,您将收到通知

检查,它覆盖了它的深度

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