FlutterFire does signs user out but does not navigate to signin screen

Viewed 18

I am using FlutterFire AuthUI in my Flutter app. In the app root I use AuthGate widget that listens to the FirebaseAuth.instance.authStateChanges() to decide to show the sign in page or the home page.

Everything works fine, but when I sign in from a screen other than the home page, the user is signed out but the screen does not switch to the sign in page again. When I sign out from the home page it works as expected.

This is my AuthGate:

  Widget build(BuildContext context) {
    return StreamBuilder<User?>(
      stream: FirebaseAuth.instance.authStateChanges(),
      builder: ((context, snapshot) {
        if (!snapshot.hasData) {
          return SignInScreen(
            providerConfigs: [
              EmailProviderConfiguration(),
            ],
          );
        }
        return TimelineScreen();
      }),
    );
  }

This is how I use it in the app root:

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

  runApp(ChangeNotifierProvider<AppUser>(
    create: (context) => AppUser(),
    child: MaterialApp(
      home: AuthGate(),
    ),
  ));
}

Signing out from a page other than the home page:

                SettingsTile(
                    title: "Sign out",
                    trailing: Icon(Icons.logout),
                    onPressed: (context) {
                      FirebaseAuth.instance.signOut();
                    }),
1 Answers

signOut() is async method, adding await should do the trick

 SettingsTile(
         title: "Sign out",
         trailing: Icon(Icons.logout),
         onPressed: (context) async {
          await FirebaseAuth.instance.signOut();
       }),
Related