[英]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.