0
String ok ="/login";
  String _loggedInFunction(){
    FirebaseAuth.instance
        .authStateChanges()
        .listen((User? user) {
      if (user == null) {
         ok = "/login";

      }else{ ok = "/home";}

      print(ok);


      print(user);

    });
    print("Reading 1st");
    return ok;
  }

I can't be able to understand whether the FirebaseAuth.instance.... is an asynchronous function. Because second statement is running before the first one, but how can I apply "await" keyword here, While applying "await" keyword before FirebaseAuth.instance... .it's showing it is not a Future event.

And also I can you explain me why I can't be able return "ok" variable which is inside if statement directly, it's showing me an error.

I used this code to skip the login if the user restart the app. Do you have any recommended code for this purpose??

Output of the above code

I/flutter ( 6405): Reading 1st
I/flutter ( 6405): /home
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • 2
    [This answer](https://stackoverflow.com/questions/45353730/firebase-login-with-flutter-using-onauthstatechanged) might be helpful to understand what onAuthStateChanged does – Dharmaraj Jul 31 '21 at 13:48

1 Answers1

2

The FirebaseAuth.instance.authStateChanges().listen is not a promise but a listener. That means you can't await it or use then to wait on it. It will trigger each time the auth state changes. You are just listening to those changes.

Tarik Huber
  • 7,061
  • 2
  • 12
  • 18