简体   繁体   中英

Flutter Firebase authStateChanges Snapshot Always Return True Even User Deleted

Firebase Flutter authStateChanges snapshot.data always return true,
Even i deleted the user from Firebase Authentication.
I readed some article that they said Firebase still store the token and will refresh it after 1 Hours,
But i wait 6 Hours the authStateChanges snapshot still returning true
Is that any wrong with my code or my Stream Builder?
Or how can i make a private route in Flutter to see that client is logged in or not,
Like if they not logged in they will redirected to Login page, and if they logged in they will redirected to Homepage etc

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(const MainApp());
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: StreamBuilder(
        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (BuildContext context, snapshot) {
          if (snapshot.connectionState == ConnectionState.active) {
            if (snapshot.hasData) {
              print('Snapshot => $snapshot');
              return HomeScreen();
            } else {
              print('Not Logged In!!!');
              return GetStartedScreen();
            }
          } else {
            return Text('Loading...');
          }
        },
      ),
    );
  }
}

Firebase authStateChanges 代码 颤振调试控制台

To be honest, I don't know exactly the answer to your specific problem, but I can advice you to refactor the builder part like:

if (snapshot.connectionState == ConnectionState.waiting) {
             return Center(child: Text("Is Loading...");
}
 else if (snapshot.hasError){
return Center(child: Text("Error: ${snapshot.error}");
}
          else  if (snapshot.hasData) {
              print('Snapshot => $snapshot');
              return HomeScreen();
            } else {
              print('Not Logged In!!!');
              return GetStartedScreen();
            }
          } 
        },

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM