0

I am trying to auto login user and Navigate the user to either Admin, Student or Teacher page after loging in with firebase and fetching there data successfull, everything is good except when I added the three choices 'Admin, Student or Techer' in an initState with three if statements It doesn't do autologin but when I replace them with one process It works So I don't know what is the proplem, every thing is fine, it finds the user, It fetchs its data and prints to be sure and navigates according to the userType which is not null, but when I do restart It doesn't do autologin. My code :

@override
  void initState() {
    super.initState();
    print(userType);
    FirebaseAuth.instance.currentUser().then((res) {
      print(res);
      if (res != null) {
        if (userType == 'Student') {
          Navigator.pushReplacementNamed(context, '/StudentsPage');
        } else if (userType == 'Teacher') {
          Navigator.pushReplacementNamed(context, '/TeacherPage');
        } else if (userType == 'Admin') {
          Navigator.pushReplacementNamed(context, '/AdminPage');
        }
      }
omar developer
  • 189
  • 1
  • 3
  • 13
  • What is the output of `print(userType)` and `print(res)`? Till which point the code is getting executed? does it enter `if(res != null)` ? Also check [this for Navigating](https://stackoverflow.com/a/44271936/13625305) from `initState` to other pages. – dev-aentgs Jun 26 '20 at 10:14
  • I think you need to put `async` and `await` there. Because if guess right, your code will always `null`. – hisam Jun 27 '20 at 07:04
  • userType is the type of the user on the firestore and It prints it succeessfully, about res it's to check that there is a firebase user already logged in to do autologin. and about async and await no, I have this same code in the login page as I logged in with the email and searched in the firestore with the username I asked and It works just fine. – omar developer Jun 27 '20 at 10:56
  • I will explain it simply, I already have an autologin in the login page but I don't want the user to login everytime tha'ts 1 proplem and the second that when I do an if inside another It doesn't do autologin but when I do 1 if It successfully do it. – omar developer Jun 27 '20 at 10:58
  • about navigating inside the initState to another pages I tried all the Navigator methods but no one did the trick. – omar developer Jun 27 '20 at 10:59
  • I have an idea about avoiding doing login everytime and that is to get the username already searched 'if logged before' but I tried so many times with different solutions but couldn't, have any ideas about doing this. – omar developer Jun 27 '20 at 11:04
  • I haven't tried this but can you try [Flutter - Role-based authorization with Firestore](https://youtu.be/Tyk0Qte0moY?t=1280) . – dev-aentgs Jul 01 '20 at 15:28
  • 1
    No it didn't help but thanks. – omar developer Jul 02 '20 at 04:56

0 Answers0