Error
NoSuchMethodError: The method 'currentUser' was called on null.
Receiver: null
Tried calling: 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)
I had also tried to using:
widget.auth.current....
root_page
In bottom of the file i had tried different approach. Instead of HomePage then _HomePageState(comments out)
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
This is the class I'm using to get current user from 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
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;
});
});
}