简体   繁体   中英

When I open my flutter app it always shows Sign In screen for few seconds, even if I already signed In in my last session. And after that Main page?

final _navigatorKey = GlobalKey<NavigatorState>();

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

  @override
  _MainAppState createState() => _MainAppState();
}

class _MainAppState extends State<MainApp> {

  @override

  Widget build(BuildContext context) {

    return GetMaterialApp(

        localizationsDelegates: context.localizationDelegates,

        supportedLocales: context.supportedLocales,

        locale: context.locale,

        navigatorKey: _navigatorKey,

        color: Colors.white,

        getPages: router.routes,

        initialRoute: SignInPageRoute,

        routingCallback: (routing) {

          final _navigationService = locator<NavigationService>();

          final _authService = locator<AuthService>();

          if (routing!.current == SignInPageRoute) {

            WidgetsBinding.instance!.addPostFrameCallback(
              (_) {
                bool isLoggedIn = _authService.isLoggedIn();
                if (isLoggedIn) {
                  _navigationService.offAllTo(MainPageRoute);
                } else {
                  return;
                }
              },
            );
          }
        });
  }
}

试试这个

bool isLoggedIn = await _authService.isLoggedIn();

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