Hassan Ansari
Hassan Ansari

Reputation: 314

I'm getting an error NoSuchMethodError: The method currentUser was called on null.FLUTTER

I'm trying to check whether the user is signed in or not, I've authenticated my flutter app with firebase, I want the status of the user to redirect the app to either login page or home page, but I can't run the app it shows the error on red screen and the error is: NoSuchMethodError: The method 'currentUser' was called on null. Receiver: null Tried calling: currentUser()

I saw the solution here but I did not understood it properly

currentUser function:

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

checking the authentication status:

void initState() {
super.initState();
  try {
    widget.auth.currentUser().then((userId) {
    setState(() {
    authStatus =
    userId == null ? AuthStatus.notsignedIn : AuthStatus.signedIn;
   });
  });
} catch (e) {}
}

Upvotes: 0

Views: 370

Answers (1)

Swift
Swift

Reputation: 3410

The error just means that your _firebaseAuth object is null. Try using

FirebaseUser user = await FirebaseAuth.instance.currentUser();

Upvotes: 1

Related Questions