user12612479
user12612479

Reputation:

Flutter Provider: Firebase Auth StateChanges is listened but state is not changing

I'm currently use Firebase Authentication service on my Flutter project. This project use Provider as its state management. After setting up sign in and sign up services, they are all work well. But as you can see, the wrapper.dart file seems to listen the AuthStateChanges() method through provider from main.dart (as the parent). Even tough it listens, the if else statement inside wrapper doesn't rebuilt if the state changes. Resulting, the homepage doesn't automatically pushed if sign in or sign up method succeded. But, it does indeed work when I hot restart the application.

My objectives is whenever the Sign In process succeed, the wrapper if else statement is listen and obviously rebuild so that the HomePage() for instance can be pushed in realtime.

Here's my project repository link on Github: https://github.com/fullstack-dre/packme

Can anybody help me here? Perhaps on how should I implement the providers? Thank you very much.

Upvotes: 1

Views: 705

Answers (1)

jamesdelacruz
jamesdelacruz

Reputation: 217

you need to import first firebase_auth.dart

import 'package:firebase_auth/firebase_auth.dart';

Then use authStateChanges() not AuthStateChanges()

you can authenticate using this code.

FirebaseAuth.instance
  .authStateChanges()
  .listen((User user) {
    if (user == null) {
      print('User is currently signed out!');
    } else {
      print('User is signed in!');
    }
  });

Upvotes: 1

Related Questions