dart 如何根据用户是否被授权使用软件包auto_route来显示所需的屏幕:7.8.0?

laximzn5  于 2023-09-28  发布在  其他
关注(0)|答案(1)|浏览(119)

我使用导航包auto_route:^7.8.0。
如何使用它来显示初始登录页面,这取决于用户是否登录到firebase?

main.dart

final _appRouter = AppRouter();

  @override
  Widget build(BuildContext context) {
    return GlobalBlocInjector(
      child: MaterialApp.router(
        localizationsDelegates: const [
          S.delegate,
          GlobalMaterialLocalizations.delegate,
          GlobalWidgetsLocalizations.delegate,
          GlobalCupertinoLocalizations.delegate,
        ],
        routerConfig: _appRouter.config(
          reevaluateListenable: ReevaluateListenable.stream(FirebaseAuth.instance.authStateChanges()),
          navigatorObservers: () => [
            TalkerRouteObserver(GetIt.I<Talker>()),
          ],
        ),
        locale: const Locale('ru'),
        supportedLocales: S.delegate.supportedLocales,
        theme: const DarkThemeBuilder().build(),
        scaffoldMessengerKey: GetIt.instance<SnackBarService>(),
      ),
    );
  }
}

app_router.dart

import 'package:auto_route/auto_route.dart';
import '../../presentation/presentation.dart';
import '../app.dart';

part 'router.gr.dart';

@AutoRouterConfig()
class AppRouter extends _$AppRouter {

  @override
  List<AutoRoute> get routes => [
        AutoRoute(page: SignInRoute.page, initial: true,),
        AutoRoute(page: SignUpRoute.page),
        AutoRoute(page: MainRoute.page),
        AutoRoute(page: ResetPasswordRoute.page),
        AutoRoute(page: ChangePasswordRoute.page),
        AutoRoute(page: ReVerificationRoute.page),
      ];
}

我试图按照pub.dev上列出的文档进行操作,但不幸的是没有任何结果

@AutoRouterConfig()
class AppRouter extends $AppRouter implements AutoRouteGuard {
  @override
  void onNavigation(NavigationResolver resolver, StackRouter router) {
      if(isAuthenticated || resolver.route.name == LoginRoute.name){
         // we continue navigation
          resolver.next();
      }else{
          // else we navigate to the Login page so we get authenticateed
          
          // tip: use resolver.redirect to have the redirected route
          // automatically removed from the stack when the resolver is completed
          resolver.redirect(LoginRoute(onResult:(didLogin)=> resolver.next(didLogin)))
      }
   }
  // ..routes[]
  }
lymnna71

lymnna711#

您可以检查

if(snapshot.hasData)
{
  //Got to homepage
}
else
{
// got to Login page
}

这更容易实现。

相关问题