0

我正在 Riverpod 中创建一个简单的单值程序。有一个浮动操作按钮,单击时会增加计数器值。

这是MyHomePage小部件:

final provider = StateNotifierProvider((ref) => CounterNotifier());

class MyHomePage extends HookWidget {
  @override
  Widget build(BuildContext context) {
    final counterModel = useProvider(provider.state);
    return Scaffold(
      appBar: AppBar(
        title: Text('Counter'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text('You have pushed the button this many times'),
            Text('${counterModel.count}',
                style: Theme.of(context).textTheme.headline4),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          context.read(provider).increment();
        },
        child: Icon(Icons.add),
      ),
    );
  }
}

然后将其作为 Windows 应用程序运行。

在此处输入图像描述

4

1 回答 1

1

对于我的情况,我在HookWidget 的build方法之外分配了useProvider()方法。

于 2021-04-11T11:21:25.757 回答