-This is the Login Firebase Function used

Simple Login Function from firebase that signs in first and checks for email verification, but when the snack bar for successful logins shows up still the widget do not rebuild as the logic in the main.dart file using provider state management

  class FirebaseUserAuthentication {
  final FirebaseAuth _auth;

  FirebaseUserAuthentication(this._auth);

//State Management

  Stream<User?> get authState => _auth.authStateChanges();
  User get user => _auth.currentUser!;

Future<void> signIn(
          {required String email,
          required String password,
          required BuildContext context}) async {
        try {
          await _auth.signInWithEmailAndPassword(email: email.toString().trim(), password: password.toString().trim());
          if (_auth.currentUser!.emailVerified == false) {
            await sendEmailVerificaiton(context);
          }
          showSnackBar(context, "Logged in Successfully");
        } on FirebaseAuthException catch (e) {
          if (e.code != null) {
            showSnackBar(context, e.message!);
          }
        }
      }}

-This is the main.dart class wrapper using provider class

The AuthWrapper class checks the user state using provider and if logged in then redirects to Homepage but for me, it never re-directs. It does prints the "Logged in state" command but never actually navigates directly. I am a beginner in this, so confused why on restarting the app this gets me directly to logged in page but not like this. I can make it work via Navigator but i guess that would be a bad approach.

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        Provider<FirebaseUserAuthentication>(create: (_) => FirebaseUserAuthentication(FirebaseAuth.instance)),
        StreamProvider(create: (context) => context.read<FirebaseUserAuthentication>().authState, initialData: null, ),
      ],
      child: MaterialApp(
        title: "My App",
        home: AuthWrapper(),
        routes: {
          '/splash': (context) => SplashScreen(),
          '/welcome': (context) => WelcomePage(),
          '/login': (context) => LoginPage(),
          '/register': (context) => RegisterPage(),
          '/home' : (context) => HomePage(),
          '/marketplace' : (context) => MarketPlacePage(),
        },

        debugShowCheckedModeBanner: false,
      ),
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    final firebaseUser = context.watch<User?>();
    if(firebaseUser!=null)
    {
      print("Logged in State");
      return HomePage();
    }
    print("Logged out State");
    return WelcomePage();

  }
}
0

There are 0 best solutions below