How to check if the user is logged in, if so show other screen?

后端 未结 3 545
夕颜
夕颜 2021-02-01 06:08

My first screen is a login screen and it needs to check if the user is logged in to open the home screen directly but I get an error using this check.

I\'m doing the che

3条回答
  •  南笙
    南笙 (楼主)
    2021-02-01 06:28

    This is how I did it. First acquire current user. If the user is not logged in the value is null otherwise, the user is logged in.

    // Get the firebase user
    User firebaseUser = FirebaseAuth.instance.currentUser;
    Widget firstWidget;
    
    // Assign widget based on availability of currentUser
    if (firebaseUser != null) {
      firstWidget = Home();
    } else {
      firstWidget = LoginScreen();
    }
    
    // Run the app with appropriate screen
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'UniClass',
      theme: ThemeData(
        primaryColor: kPrimaryColor,
        scaffoldBackgroundColor: Colors.white,
      ),
      home: firstWidget,
    );
    

提交回复
热议问题