The expression doesn't evaluate to a function, so it can't be invoked when using auth.currentUser

Viewed 11323

I'm new to flutter... I'm trying to get the state of the currently loggedIn user and notify the listeners using firebase but I'm getting this error The expression doesn't evaluate to a function, so it can't be invoked. at the auth.currentUser but I don't understand why.

Below is my code.

_initialize() async{
    SharedPreferences prefs = await SharedPreferences.getInstance();
    bool loggedIn = prefs.getBool(LOGGED_IN) ?? false;
    if(!loggedIn){
      _status = Status.Unauthenticated;
    }else{
      await auth.currentUser().then((currentUser) async{
        _user = currentUser;
        _status = Status.Authenticated;
        _userModel = await _userServices.getUserById(currentUser.uid);
      });

    }
    notifyListeners();
  }
}
1 Answers

It looks like you're using an outdated tutorial with the latest libraries. currentUser is a property nowadays, and not a method. There is also no longer a need to await it, so:

if(!loggedIn){
  _status = Status.Unauthenticated;
}else{
  _user = auth.currentUser;
  _status = Status.Authenticated;
  _userModel = await _userServices.getUserById(currentUser.uid);
}

Also see the FlutterFire documentation on using authentication and currentUser() is not working type why I am seeing the expression doesn't evaluate to a function, so it can't be invoked.

Related