0

I am trying to do autologin with flutter and firebase and put it in an initState but it doesn't seem to be excuted because I tried to move it to Build Widget but nothing worked but when I put it inside LoginPageState It was excuted but with error : Navigator operation requested with a context that does not include a Navigator. My Code :

@override
  void initState() {
    super.initState();
    final user = FirebaseAuth.instance.currentUser();
    if (user == null) {
      print('User Null');
    } else {
      Navigator.pushReplacementNamed(context, '/HomePage');
    }
  }
OKKO
  • 33
  • 8
  • try create a function outside it and put the code inside , and then call it inside initstate – azdeviz Aug 12 '20 at 19:35
  • try create a function outside it and put the code inside , and then call it inside initstate – azdeviz Aug 12 '20 at 19:35
  • Can you try to put your code inside ? WidgetsBinding.instance.addPostFrameCallback((timeStamp) { inal user = FirebaseAuth.instance.currentUser(); if (user == null) { print('User Null'); } else { Navigator.pushReplacementNamed(context, '/HomePage'); } }); – ikerfah Aug 12 '20 at 19:47
  • Doing it inside a function didn't work. – OKKO Aug 12 '20 at 19:52

1 Answers1

1

You need to wait for the FirebaseAuth.instance.currentUser() to finish. So use await as the following.

final user = await FirebaseAuth.instance.currentUser()
Sanjay Sharma
  • 3,687
  • 2
  • 22
  • 38
  • 1
    It worked except for adding async to initState because it is imossible so I added async to the **autologin()** function. Thanks :) – OKKO Aug 12 '20 at 20:25