Flutter自动路线重新评估列出仅工作一次

yr9zkbsy  于 2023-11-21  发布在  Flutter
关注(0)|答案(1)|浏览(135)

我正在使用reevaluateListable来收听我的设置Cubit Streem(自动路由Flutter包),它只做了一次,并且不会再次工作,直到我再次刷新应用程序。
这是我的应用dart

class MizanitiApp extends StatelessWidget {
  MizanitiApp({super.key});

  final AppRouter router = AppRouter();

  @override
  Widget build(BuildContext context) {
    return MultiBlocProvider(
      providers: [
        BlocProvider(
            lazy: false,
            create: (context) => locator<SettingsCubit>()..initiated()),
        BlocProvider(
            create: (context) => locator<WalletsWatcherCubit>()..initiated()),
      ],
      child: BlocConsumer<SettingsCubit, SettingsState>(
        listenWhen: (previous, current) =>
            previous.failureOrSuccessOption != current.failureOrSuccessOption,
        listener: (context, state) {
          state.failureOrSuccessOption.fold(
            () => null,
            (either) => either.fold(
              // failure
              (Failure failure) =>
                  _onSettingsFailure(context: context, failure: failure),
              // success
              (_) => _onSettingsSuccess(context: context),
            ),
          );
        },
        buildWhen: (previous, current) =>
            previous.domain.theme != current.domain.theme,
        builder: (context, state) {
          return MaterialApp.router(
            title: 'Mizaniti App',
            themeMode: state.domain.theme.getOrNull(),
            darkTheme:
                ThemeData(useMaterial3: true, brightness: Brightness.dark),
            theme: ThemeData(
              colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
              useMaterial3: true,
            ),
            routerConfig: router.config(
                reevaluateListenable: ReevaluateListenable.stream(
                    context.read<WalletsWatcherCubit>().stream)),
          );
        },
      ),
    );
  }
}

字符串
这是我的守卫

class BiometricGuard extends AutoRouteGuard {
  @override
  void onNavigation(NavigationResolver resolver, StackRouter router) async {
    final BuildContext? context = router.navigatorKey.currentContext;

    optionOf(context).fold(
      () => resolver.next(),
      (context) {
        print('fingerprint');
        final bool isAppGuarded =
            context.read<SettingsCubit>().state.isAppGuarded;

        if (isAppGuarded) {
          router.replaceAll([const BiometricGuardRoute()]);
          return;
        }

        resolver.next();
      },
    );
  }
}

e5nqia27

e5nqia271#

我遇到了同样的问题。不确定我的解决方案是否正确,但在我的情况下,我通过在强制重定向之前添加resolver.redirect(const LoginRoute())来解决它。
这是我的代码,如果你感兴趣。

@override
void onNavigation(NavigationResolver resolver, StackRouter router) async {
  if (router.isRoot) {
    switch (notifier.status) {
      case AuthStatus.loggedOut:
        if (resolver.routeName != LoginRoute.name) {
          resolver.next(false);
          resolver.redirect(LoginRoute(onResult: (p0) {}));
          return;
        }
      case AuthStatus.none:
        if (resolver.routeName != SplashRoute.name) {
          resolver.next(false);
          resolver.redirect(const SplashRoute());
          return;
        }
      case AuthStatus.loggedIn:
        if (resolver.routeName != HomeRoute.name) {
          resolver.next(false);
          resolver.redirect(const HomeRoute());
          return;
        }
    }
  }
  resolver.next();
}

字符串

相关问题