当我按下后退按钮并重新打开我的应用程序时,它会返回到登录屏幕,但用户仍处于登录状态

When I press back button and reopen my app it goes back to login screen but user is still logged in

当我按下后退按钮并重新打开我的应用程序时,它会返回到登录屏幕,但用户仍处于登录状态。我正在使用 navigator.pushandremoveuntill 当用户登录时,我的应用程序工作正常,直到我重新打开我的应用程序我被带回登录页面,这不应该发生,因为我使用 navigator.pushandremoveuntill 并删除了所有后台堆栈屏幕。谁能帮帮我。

这是我的代码

  signIn() async {
    if (emailController.text.isNotEmpty &&
        passwordController.text.isNotEmpty &&
        passwordController.text.length >= 6) {
      setState(() {
        isClicked = true;
      });
      await FirebaseFunctions()
          .signIn(emailController.text, passwordController.text, context);

      if (FirebaseAuth.instance.currentUser != null) {
        Navigator.pushAndRemoveUntil(
            context,
            MaterialPageRoute(builder: (context) => const HomeScreen()),
            (route) => false);
      }
    } else {
      timerSnackbar(
          backgroundColor: Colors.red,
          buttonLabel: "",
          context: context,
          contentText: "Please Check your Credentials",
          second: 2,
          afterTimeExecute: () {});
      setState(() {
        isClicked = false;
      });
    }
  }

这是主应用程序


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

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: FirebaseAuth.instance.currentUser != null
          ? const HomeScreen()
          : const SelectionScreen(),
      debugShowCheckedModeBanner: false,
    );
  }
}

Firebase SDK 会自动保留用户的凭据,并在应用重启时尝试恢复它们。然而,这需要它调用服务器(例如检查帐户是否被禁用),这需要一些时间,在此期间 FirebaseAuth.instance.currentUser 将是 null。在此期间,您的 home: FirebaseAuth.instance.currentUser != null 运行,并正确确定尚未有用户登录,并呈现 SelectionScreen.

解决方案是使用 auth state listener, similar to what I show in this article for Android: Listen for authentication state in Android。由于这是一个 Stream,您可以将它与小部件树中的 StreamBuilder 连接起来,让您的 UI 响应身份验证状态中的任何更改。

您应该为此目的使用 SteamBuilder。 只需根据您的需要编辑以下代码:

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
  runApp(MaterialApp(
      debugShowCheckedModeBanner: false,
      title: 'Flutter Firebase',
      theme: ThemeData(
        primarySwatch: Colors.green,
      ),
      initialRoute: "/",
      routes: {
        "/": (context) => directPage(),
      },
    ),
  );
}

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

  @override
  Widget build(BuildContext context) {
    return StreamBuilder(
        stream: FirebaseAuth.instance.authStateChanges(),
        builder: (_,snapshot){
          if(snapshot.hasData){
            return pageOne(); //your page class you want to show when you are logged in. Like showing the welcome page
          }else{
            return pageTwo(); //your page class you want to show when you are not logged in. Like showing the login page
          }
        }
    );
  }
}