【问题标题】:Error: Could not find the correct Provider<Cart> above this Consumer<Cart> Widget错误:在此 Consumer<Cart> 小部件上方找不到正确的 Provider<Cart>
【发布时间】:2021-01-07 13:37:04
【问题描述】:

我将提供程序放在材料应用程序之上,这样我就可以在应用程序内的每个小部件中使用它,对吗? 那么为什么这个错误 我的代码是

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider.value(
          value: Cart(),
        ),
        ChangeNotifierProvider.value(value: ProductsProvider()),
      ],
      child: MaterialApp(
        debugShowCheckedModeBanner: false,
        title: 'Flutter Demo',
        theme: ThemeData(
          primarySwatch: Colors.purple,
          accentColor: Colors.deepOrange,
          fontFamily: 'Lato',
          visualDensity: VisualDensity.adaptivePlatformDensity,
        ),
        home: ProductOverviewScreen(),
        routes: {ProductDetailScreen.routeName: (ctx) => ProductDetailScreen()},
      ),
    );
  }
}

这个屏幕有错误

enum filterOptions { Favorites, All }

class ProductOverviewScreen extends StatefulWidget {
  @override
  _ProductOverviewScreenState createState() => _ProductOverviewScreenState();
}

class _ProductOverviewScreenState extends State<ProductOverviewScreen> {
  var _showOnlyFavorites = false;

  @override
  Widget build(BuildContext context) {
    //final cart = Provider.of<Cart>(context);
    return Scaffold(
        appBar: AppBar(
          title: Text("MyShop"),
          actions: [
            PopupMenuButton(
                onSelected: (selectedValue) {
                  setState(() {
                    if (selectedValue == filterOptions.Favorites) {
                      _showOnlyFavorites = true;
                    } else if (selectedValue == filterOptions.All) {
                      _showOnlyFavorites = false;
                    }
                  });
                },
                icon: Icon(Icons.more_vert),
                itemBuilder: (_) => [
                      PopupMenuItem(
                          child: Text("Only Favorites"),
                          value: filterOptions.Favorites),
                      PopupMenuItem(
                          child: Text("Show All"), value: filterOptions.All),
                    ]),
            Consumer<Cart>(
              builder: (_, cartData, ch) => Badge(
                child: ch,
                value: cartData.itemCount.toString(),
              ),
              child: IconButton(
                icon: Icon(Icons.shopping_cart),
                onPressed: () {},
              ),
            )
          ],
        ),
        body: ProductsGrid(_showOnlyFavorites));
  }
}

消费者的错误是 错误:在此消费者小部件上方找不到正确的提供者 为什么这个屏幕不能知道购物车提供者? 有什么帮助吗?

【问题讨论】:

    标签: flutter dart flutter-provider


    【解决方案1】:

    我没有足够的声誉来发表评论,但是您的示例在我使用颤振 1.22.5 和提供程序 4.3.2 时运行良好。但是,当意外导入名为 flutter_provider 的包并使用其 Consumer 小部件时,我设法重现了您的问题。无法想象这是你的问题。

    顺便说一句,you should avoid 使用value 构造函数来创建您的ChangeNotifier。传递变量或使用带有create 参数的默认构造函数。

    【讨论】:

    • 感谢您的关注,问题是通过在 android studio 中使用 alt+enter 自动导入购物车小部件,它必须使用 ../
    猜你喜欢
    • 2020-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-02-04
    • 2021-01-31
    • 2020-08-21
    • 2021-07-23
    • 2020-10-07
    相关资源
    最近更新 更多