Search code examples
flutterfirebasedartdart-null-safety

Flutter Dart Firebase error: The function can't be unconditionally invoked because it can be 'null'


I want to check if the user is currently registered, but I get a problem with the not null errors. Please have a look at my code:

class _ChatScreenState extends State<ChatScreen> {final _auth = FirebaseAuth.instance; void getCurrentUser() async {
final user = await _auth!.currentUser();}

enter image description here


Solution

  • you're calling it as a Function, but you can get the current user information directly with currentUser, not currentUser():

    final _auth = FirebaseAuth.instance;
    _auth.currentUser(); // wrong way
    _auth.currentUser; // right way
    

    currentUser() doesn't exist, currentUser does.