在 Gridview.builder 中使用 ChangeNotifierProvider.value 时遇到错误
Encountering error while using ChangeNotifierProvider.value in Gridview.builder
This is likely a mistake and is unsupported.
If you're in this situation, consider passing a key
unique to each individual constructor.
以上是反映 运行 带有 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,
),
)
这种情况可以通过使用指定的键来解决:-
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,
),
)
This is likely a mistake and is unsupported. If you're in this situation, consider passing a
key
unique to each individual constructor.
以上是反映 运行 带有 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,
),
)
这种情况可以通过使用指定的键来解决:-
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,
),
)