flutternavigation

Why can't I use a variable for the homepage in Flutter?


I'm trying to bypass the Login page by using a variable for the homepage widget. I check FirebaseAuth to determine if the user is signed in already. Then I set the homepage based on yes or no. But it gives me a blank white screen. Why doesn't this work?

    @override
      Widget build(BuildContext context) {
    
        Widget? home;

    //Check to see if a user is already logged in.
    FirebaseAuth.instance
      .authStateChanges()
      .listen((User? user) {
        if (user == null) {
          home = Signup();
        } else {
          home = const HomePage();
        }
      });

    return MaterialApp(
      navigatorKey: NavigationService.navigatorKey,
      debugShowCheckedModeBanner: false, // turn off debug banner
      title: 'My App',
     
      home: home,
    );
   }
 }

Solution

  • Since you have not provided much code to understand the issue.Here is full code to achieve your desired way.

    import 'package:flutter/material.dart';
    import 'package:firebase_core/firebase_core.dart';
    import 'package:firebase_auth/firebase_auth.dart';
    
    Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      runApp(MyApp());
    }
    
    class MyApp extends StatefulWidget {
    const MyApp({super.key});
    
    @override
    State<MyApp> createState() => _MyAppState();
    }
    
    class _MyAppState extends State<MyApp> {
     // Navigator key for managing navigation globally
     final navigatorKey = GlobalKey<NavigatorState>();
    
     Widget? home;
    
     void checkLogin() {
       // Check to see if a user is already logged in.
      FirebaseAuth.instance.authStateChanges().listen((User? user) {
        if (user == null) {
          home = Signup();
        } else {
         home = const HomePage();
       }
      setState(() {});
      });
    }
    
    @override
    void initState() {
      super.initState();
     checkLogin();
    }
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          navigatorKey: navigatorKey,
          debugShowCheckedModeBanner: false, // Turn off debug banner
          title: 'My App',
          home: home ?? LoadingScreen(),
        );
      }
    }
    
    class NavigationService {
      static final navigatorKey = GlobalKey<NavigatorState>();
    }
    
    // Placeholder for Signup screen
    class Signup extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Signup'),
          ),
          body: Center(
            child: Text('Signup Screen'),
          ),
        );
      }
    }
    
    // Placeholder for Home screen
    class HomePage extends StatelessWidget {
      const HomePage({super.key});
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: const Text('Home'),
          ),
          body: Center(
            child: Text('Welcome to the Home Page!'),
          ),
        );
      }
    }
    
    // Placeholder for Loading screen
    class LoadingScreen extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Center(
            child: CircularProgressIndicator(),
          ),
        );
      }
    }