Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/rensawamo/all_riverpods
https://github.com/rensawamo/all_riverpods
Last synced: 10 days ago
JSON representation
- Host: GitHub
- URL: https://github.com/rensawamo/all_riverpods
- Owner: rensawamo
- Created: 2024-02-02T01:36:43.000Z (10 months ago)
- Default Branch: main
- Last Pushed: 2024-02-04T06:27:02.000Z (10 months ago)
- Last Synced: 2024-02-04T07:28:04.437Z (10 months ago)
- Language: Dart
- Size: 183 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Riverpodを扱う widget
floatingActionButton の中などに埋め込める## Consumer
```sh
Consumer(builder: (context, ref, child) {
return Text(
'${ref.watch(_counterProvider)}',
style: Theme.of(context).textTheme.headline4,
);
}),
```## ConsumerWidget
widgetの定義時に購読できるように
```sh
class MyHomePage extends ConsumerWidget {
MyHomePage({super.key, required this.title});
final String title;final _counterProvider = StateProvider((ref) => 0);
@override
Widget build(BuildContext context, WidgetRef ref) {
(中略)
}
}
```## ConsumerStatefulWidget
build widget の中で値の変更を行うための refを発行する
```sh
class MyHomePage extends ConsumerStatefulWidget {
@override
ConsumerState createState() => _MyHomePageState();
}class _MyHomePageState extends ConsumerState {
final _counterProvider = StateProvider((ref) => 0);@override
Widget build(BuildContext context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () =>
ref.read(_counterProvider.notifier).update((state) => state + 1),
child: const Icon(Icons.add),
),
);
}
}
```# Riverpodのmethod
## read
その時点での状態を取得## watch
状態を監視し続ける。
リアクティブな値を埋め込むのに使用