When I open my flutter app it always shows SignIn screen for few seconds, even if I already signed In in my last session. And after that Main page

321

This is happening because you are using controlSignIn method to determine whether the user is logged in or not. As it is an async function, it is taking some time to determine the signed-in user. Hence you are able to see the sign-in page for a second, then the home page.

You can fix this by checking this logic in the splash screen & then navigating to the appropriate page - sign in or home.

Create a new file: splash_page.dart. Your SplashPage should look something like this:

class SplashPage extends StatefulWidget {
  @override
  _SplashPageState createState() => _SplashPageState();
}

class _SplashPageState extends State<SplashPage> {
  @override
  void initState() {
    super.initState();
    // your logic to check whether user is signed in or not
    // If user is logged in navigate to HomePage widget
    // Else navigate to SignInPage
  }

  @override
  Widget build(BuildContext context) {
    return const Center(child: CircularProgressIndicator());
  }
}
Share:
321
Harsh Patel
Author by

Harsh Patel

Updated on December 20, 2022

Comments

  • Harsh Patel
    Harsh Patel over 1 year

    When I start my flutter app, it starts with sign in screen even if I already logged in. And automatically after few seconds it open the main page.

    My code: My main build:-

    @override
    Widget build(BuildContext context) {
    
    //saveUserInfoToFireStore();
    if(isSignedIn)
    {
      return buildHomeScreen();
    }
    else{
      return buildSignInScreen();
    }
    }
    

    This is my init function:-

    bool isSignedIn = false;
    void initState() {
    super.initState();
    
    pageController = PageController();
    
    gSignIn.onCurrentUserChanged.listen((gSigninAccount){
      controlSignIn(gSigninAccount);
    }, onError:(gError){
      print("Error Message: " +gError);
    });
    
    gSignIn.signInSilently(suppressErrors: false).then((gSignInAccount){
      controlSignIn(gSignInAccount);
    
    }).catchError((gError){
      print("Error Message: "+gError); // + gerror
    });
    }
    
    controlSignIn(GoogleSignInAccount signInAccount) async {
    if(signInAccount != null)
      {
    
         final GoogleSignInAccount gCurrentUser = gSignIn.currentUser;
         DocumentSnapshot documentSnapshot = await usersReference.doc(gCurrentUser.id).get();
         currentUser = User.fromDocument(documentSnapshot);
         setState(() {
           isSignedIn=true;
         });
    
    
        configureRealTimePushNotifications();
      }
    else{
      setState(() {
        isSignedIn = false;
      });
    }
    }
    

    once I signed In I want that every time I open my app it should only shows homepage, rather that SignIn page.

  • Harsh Patel
    Harsh Patel about 3 years
    How to check this logic in splash screen? can you please give a idea.