在Gridview.builder中使用ChangeNotifierProvider.value时遇到错误

时间:2020-04-04 13:06:39

标签: flutter hybrid-mobile-app flutter-dependencies flutter-change-notifier

这可能是一个错误,不受支持。 如果遇到这种情况,请考虑为每个构造函数传递唯一的key

以上是该错误反映在以flutter小部件作为子代运行代码时:-

    GridView.builder(
      padding: const EdgeInsets.all(10.0),
      itemCount: products.length,
      itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
        value: products[i],
        child: ProductItem(
            ),
      ),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        childAspectRatio: 3 / 2,
        crossAxisSpacing: 10,
        mainAxisSpacing: 10,
      ),
    )

1 个答案:

答案 0 :(得分:1)

可以通过使用指定的键来解决这种情况:-

    GridView.builder(
      padding: const EdgeInsets.all(10.0),
      itemCount: products.length,
      itemBuilder: (ctx, i) => ChangeNotifierProvider.value(
        key: Key("item$i"),
        value: products[i],
        child: ProductItem(
            ),
      ),
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
        childAspectRatio: 3 / 2,
        crossAxisSpacing: 10,
        mainAxisSpacing: 10,
      ),
    )