无法找到正确的提供商

发布于 2025-02-03 00:56:02 字数 703 浏览 3 评论 0原文

我正在进行一个实验,我想在其中使用两个源代码制作两个类似的应用程序。

我正在尝试在小部件树的顶部建立“自适应”状态,并且此状态(changeNotifier)取决于应用程序(isapp1()确定哪个应用)。

这是我正在使用的代码:

ChangeNotifierProvider(
      create: (context) {
        return isApp1() ? AppState1() : AppState2();
      },
      child: MaterialApp(
        onGenerateRoute: isAdminApp()
            ? App1Router.generateRoute
            : App2Router.generateRoute,
        initialRoute: "/",
      ),
    );

我尝试在树的低级别中使用提供商。

Error: Could not find the correct Provider<AppState1> above this Widget

当 代码> appstate2()直接而不是isapp1()? AppState1():AppState2()我不遇到此错误。

I am doing an experiment, where I want to make two similar apps with single source code.

I am trying to make an "adaptive" State on top of the widget tree, and this state (ChangeNotifier) depends on the application (isApp1() determines which app is this ).

here's the code that I'm using:

ChangeNotifierProvider(
      create: (context) {
        return isApp1() ? AppState1() : AppState2();
      },
      child: MaterialApp(
        onGenerateRoute: isAdminApp()
            ? App1Router.generateRoute
            : App2Router.generateRoute,
        initialRoute: "/",
      ),
    );

when I try to read the state using Provider.of in the low levels of the tree I am facing the error:

Error: Could not find the correct Provider<AppState1> above this Widget

Note: when I put AppState1() or AppState2() directly instead of isApp1() ? AppState1() : AppState2() I don't face this error.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文