Home > Mobile >  Checking if the user is logged in, go to home directly, if not go to sign in
Checking if the user is logged in, go to home directly, if not go to sign in

Time:11-17

This is my main. I want to check if user is logged in then I want to go to home directly, if not go to sign in. I also have a splash screen, so I want to show it if the app is run for the first time. Im trying to implement but nothing goes right.

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  Widget firstWidget;
  User? firebaseUser = FirebaseAuth.instance.currentUser;
  
  if (firebaseUser != null) {
    firstWidget = const HomeScreen();
  } else {
    firstWidget = const SplashScreen();
  }
  
  runApp(const MyApp());
}


class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);
  

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Fema App',
      theme: theme(),
       home: firstWidget, //I tried to call first widget but I can't
      // We use routeName so that we dont need to remember the name
      //initialRoute: LoadPage.routeName,
      //initialRoute: HomeScreen.routeName,
      routes: routes,
    );
  }
}

CodePudding user response:

Use NetworkImage('${user!.photoURL}'). A network image will need a string value. Your code was trying to assign a value that can be null also. Now if you add it inside a string if user is null you still have an empty string.

  • Related