我正在使用StateProvider<List<String>>来跟踪井字游戏板上的用户点击。实际板是一个扩展ConsumerWidget 的小部件,由可点击的 GridView 组成。
在GridViews 子级的onTap事件中,调用以下命令来更新状态:
ref.read(gameBoardStateProvider.notifier).state[index] = 'X';
Run Code Online (Sandbox Code Playgroud)
由于某种原因,这不会调用小部件重建事件。因此,我看不到被点击的 GridView 项目中的“X”。
但是,如果我添加额外的“简单”StateProvider<int> 并在同一 onTap 事件中调用它,那么小部件将被重建,并且我可以在 GridView 中看到“X”。我什至没有使用或显示这个附加的状态提供程序,但由于某种原因它会调用重建,而我想要的提供程序却不会。
final gameBoardStateProvider = StateProvider<List<String>>((ref) => List.filled(9, '', growable: false));
final testStateProvider = StateProvider<int>((ref) => 0); //dummy state provider
class Board extends ConsumerWidget {
const Board({Key? key}) : super(key: key);
@override
Widget build(BuildContext context, WidgetRef ref) {
final gameBoard = ref.watch(gameBoardStateProvider);
final testState = ref.watch(testStateProvider);
return Expanded(
child: Center(
child: GridView.builder(
itemCount: 9,
gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: …Run Code Online (Sandbox Code Playgroud)