简体   繁体   English

Provider Widget 的放置位置 — Flutter Provider Package

[英]Where to place a Provider Widget — Flutter Provider Package

I am currently learning app development with Flutter and have started learning about the Provider package.我目前正在学习使用 Flutter 进行应用程序开发,并已开始了解 Provider package。 I was having some difficulty and was getting the error:我遇到了一些困难并且收到了错误:

"Could not find the correct Provider above this... Widget" “在此...小部件上方找不到正确的提供程序”

I ended up moving the Provider widget to wrap around my MaterialApp widget instead of my Scaffold Widget, and that seemed to fix things.我最终将 Provider 小部件移动到环绕我的 MaterialApp 小部件而不是我的 Scaffold 小部件,这似乎解决了问题。

That being said, I'm not sure why this fixed things.话虽如此,我不确定为什么这会解决问题。 Are we supposed to put our Provider widget around our MaterialApp?我们是否应该将我们的 Provider 小部件放在 MaterialApp 周围? If so, can someone please explain why this is needed?如果是这样,有人可以解释为什么需要这样做吗? If not, can someone explain how to determine where to place the Provider widget in our tree?如果没有,有人可以解释如何确定将 Provider 小部件放置在我们的树中的位置吗?

Usually, the best place is where you moved it, in the MaterialApp.通常,最好的位置是您在 MaterialApp 中移动它的位置。 This is because since that is where the app starts, the node tree will have access to the provider everywhere.这是因为因为这是应用程序开始的地方,所以节点树将可以访问任何地方的提供者。

If your page is a Stateful widget - inside Widget wrap State with Provider, so you can use it inside of State.如果您的页面是一个有状态的小部件 - 在 Widget 内部包装 State 和 Provider,那么您可以在 State 内部使用它。 This is a much cleaner solution because you won't have to wrap your entire application.这是一个更简洁的解决方案,因为您不必包装整个应用程序。

If you need the functionality of Provider everywhere in the app - yes, wrapping the entire app is completely fine, though I'll prefer to use some kind of service for this如果您在应用程序的任何地方都需要 Provider 的功能 - 是的,包装整个应用程序是完全可以的,尽管我更愿意为此使用某种服务

You could add it to any route and pass it to the route you need to use or you can add it to MaterialApp so you can use it anywhere.您可以将其添加到任何路线并将其传递给您需要使用的路线,或者您可以将其添加到 MaterialApp 以便您可以在任何地方使用它。

The best practice of using provider:使用提供者的最佳实践:

Place the Provider widget at the top of the widget tree.将 Provider 小部件放在小部件树的顶部。 Bellow I put a template code that can be used for one more providers at the same place, by using MultiProvider widget under Provider package.下面我通过使用 Provider package 下的 MultiProvider 小部件,在同一个地方放置了一个模板代码,该模板代码可用于多个提供商。

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ProviderName<ModelName>(create: (_) => ModelName()),
        AnotherProviderName<AnotherModelName>(create: (_) => AnotherModelName()),
      ],
      child: MaterialApp(
        title: 'App title',
        theme: ThemeData(
              primarySwatch: Colors.blue,
              primaryColor: const Color(0xFF2196f3),
              accentColor: const Color(0xFF2196f3),
              canvasColor: const Color(0xFFfafafa),
        ),
        home: MyHomePage(), // Your widget starting
      ),
    );
  }
}

For more informatin: https://pub.dev/documentation/provider/latest/更多信息: https://pub.dev/documentation/provider/latest/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM