Flutter Provider上下文未使用rootNavigatorState进行侦听

cl25kdpy  于 2023-05-23  发布在  Flutter
关注(0)|答案(1)|浏览(127)

我需要访问提供程序的值,但我不能传递context,所以我创建了一个全局navigatorKey,如下所示:

final rootNavigatorKey = GlobalKey<NavigatorState>();

在我的应用里设置
现在我有一个应该监听我的提供程序的函数,但它不起作用:

String get tr {
    Locale currentLocale = Provider.of<LocaleProvider>(
      rootNavigatorKey.currentContext!,
      listen: true,
    ).currentLocale;

    return translationsKeys[currentLocale.languageCode]![this]!;
  }

就像我说的,这不是正确的监听,因为当在提供程序内部调用notifyListeners时,什么都没有发生,可见数据没有更新。但当再次上下滚动时,数据会更新。弹出视图时也是如此,数据也会更新。只有当前可见的数据不会更新。

void setLocale(Locale locale) {
    currentLocale = locale;
    notifyListeners();
  }

但是,当传递实际上下文时,它会按预期工作:

String tr2(BuildContext context) {
    Locale currentLocale = Provider.of<LocaleProvider>(
      context,
      listen: true,
    ).currentLocale;

    return translationsKeys[currentLocale.languageCode]![this]!;
  }

有没有什么方法可以解决这个问题,这样我就不需要通过context了?
我知道供应商的范围。因此,我将提供商放在我上面的应用程序中的MultiAppProvider,并尝试了这里提到的解决方案与我的goRouter

final rootNavigatorKey = GlobalKey<NavigatorState>();

class AppRouter {
  static final _shellNavigatorKey = GlobalKey<NavigatorState>();

  static final router = GoRouter(
    initialLocation: IntroView.path,
    debugLogDiagnostics: true,
    navigatorKey: rootNavigatorKey,
    routes: [
      ShellRoute(
        navigatorKey: _shellNavigatorKey,
        pageBuilder: (context, state, child) {
          return NoTransitionPage(
            child: ScaffoldView(
              child: child,
            ),
          );
        },
        routes: [
          GoRoute(
            name: ProjectsView.name,
            path: '/projects',
            parentNavigatorKey: _shellNavigatorKey,
            pageBuilder: (context, state) {
              return const FadePageTransition(
                page: ProjectsView(),
              );
            },
            routes: [
              GoRoute(
                parentNavigatorKey: rootNavigatorKey,
                path: ':projectTitle',
                pageBuilder: (context, state) {
                  final localeProvider = Provider.of<LocaleProvider>(context);
                  return FadePageTransition(
                    page: ChangeNotifierProvider.value(
                      value: localeProvider,
                      child: ProjectDetailScaffold(
                        project: Projects.values.byName(
                          state.params['projectTitle']!,
                        ),
                      ),
                    ),
                  );
                },
              ),
            ],
          ),

但是当像上面那样调用setLocale时,我得到了这个error
在构建Builder(dirty,dependencies:[_InheritedProviderScope<LocaleProvider?>]):Assert失败:file:///Users/christiankonnerth/. pub-cache/hosted/pub.dev/go_router-6.0.1/lib/src/builder.dart:136:14 builder.dart:136 _routeMatchLookUp.isEmpty不正确

8oomwypt

8oomwypt1#

在使用**[Flutter's Provider]**1包时,可能会遇到使用rootNavigatorState时上下文不侦听的问题。这是一个已知的问题,当应用中有多个嵌套导航器时可能会发生。
要解决这个问题,您可以尝试使用不同的方法来访问Provider中的上下文。您可以使用Builder小部件来创建一个新的上下文,而不是直接使用上下文,该上下文的作用域是您需要访问Provider的特定小部件子树。
通过使用Builder小部件,可以确保用于访问Provider的上下文的范围正确,并侦听更改。
如果问题仍然存在,建议提供更具体的详细信息和代码片段,以便更好地理解和解决问题。

相关问题