Riverpod โ Getting Started#
ํ๋ง๋๋ก (๋น๊ฐ๋ฐ์์ฉ)#
Riverpod์ ์ฑ ์์์ ์ฌ๋ฌ ํ๋ฉด์ด ํจ๊ป ์ฐ๋ ์ ๋ณด(์: ๋ก๊ทธ์ธ ์ํ, ์ฌ์ฉ์ ์ด๋ฆ)๋ฅผ ํ๊ณณ์ ๊น๋ํ๊ฒ ๋ณด๊ดํ๊ณ ๋๋ ์ฐ๊ฒ ํด ์ฃผ๋ ๋๊ตฌ์ ๋๋ค. ๋ง์น ์ง ์ ๊ณณ๊ณณ์ ์ฝ์ผํธ๋ฅผ ๊น์ ๋๊ณ ํ์ํ ๊ฐ์ ์ ํ์ด ์ด๋์๋ ์ ๊ธฐ๋ฅผ ๋์ด ์ฐ๋ ๊ฒ๊ณผ ๋น์ทํฉ๋๋ค. ์ด ์คํฌ์ ๊ทธ ์ฝ์ผํธ(Riverpod)๋ฅผ ์ฒ์ ์ค์นํ๊ณ , ์ ์์ ์ผ๊ณ , ๊ฐ์ฅ ๊ฐ๋จํ "Hello world" ์์ ๋ฅผ ๋๋ ค ๋ณด๋ ์ฒซ๊ฑธ์์ ์๋ดํฉ๋๋ค.
๋ฌด์์ยท์ธ์ (๋น๊ฐ๋ฐ์์ฉ)#
-
๋ฌด์์ ํด ์ฃผ๋์
- ์ฑ ํ๋ก์ ํธ์ Riverpod ํจํค์ง๋ฅผ ์ค์นํฉ๋๋ค(Flutter์ฉ ๋๋ ์์ Dart์ฉ).
- ์ฑ ์ ์ฒด๋ฅผ
ProviderScope๋ก ๊ฐ์ธ์ ์ด๋์๋ ์ ๋ณด๋ฅผ ๊บผ๋ด ์ธ ์ ์๊ฒ ์ค๋นํฉ๋๋ค. - ๊ฐ์ฅ ๊ธฐ์ด์ ์ธ ์์ ("Hello world")๋ฅผ ๋ง๋ค์ด ์ ์ ๋์์ ํ์ธํฉ๋๋ค.
- ํ์ํ๋ฉด ์ฝ๋ ์๋ ์์ฑ๊ณผ ์ฝ๋ ์ ๊ฒ(linter) ๊ธฐ๋ฅ๊น์ง ์ผ ์ค๋๋ค.
-
์ธ์ ์ฐ๋์
- Flutter ๋๋ Dart ํ๋ก์ ํธ๋ฅผ Riverpod์ผ๋ก ์๋ก ์์ํ ๋
- ๊ธฐ์กด ํ๋ก์ ํธ์ Riverpod์ ์ฒ์ ์ถ๊ฐํ ๋
ProviderScope์ ์ฒซ ๋ฒ์งธ provider๋ฅผ ์ค์ ํด์ผ ํ ๋
ํต์ฌ ์ฉ์ด (๋น๊ฐ๋ฐ์์ฉ)#
| ์ฉ์ด | ์ฌ์ด ์ค๋ช |
|---|---|
| Riverpod | ์ฑ์ด ๊ณต์ ํ๋ ์ ๋ณด๋ฅผ ํ๊ณณ์์ ๊ด๋ฆฌํ๊ณ ๋๋ ์ฐ๊ฒ ํด ์ฃผ๋ ๋๊ตฌ(์ํ ๊ด๋ฆฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ) |
| Flutter / Dart | ์ฑ์ ๋ง๋๋ ๋ฐ ์ฐ๋ ๊ธฐ์ . Dart๋ ์ธ์ด, Flutter๋ ๊ทธ ์์์ ํ๋ฉด์ ๋ง๋๋ ๋๊ตฌ |
| ํจํค์ง(package) | ๋จ์ด ๋ฏธ๋ฆฌ ๋ง๋ค์ด ๋ ๊ธฐ๋ฅ ๋ฌถ์. ์ค์นํด์ ๊ฐ์ ธ๋ค ์ฐ๋ ๋ถํ |
| ProviderScope | ์ฑ ์ ์ฒด๋ฅผ ๊ฐ์ธ ์ ๋ณด๋ฅผ ์ด๋์๋ ๊บผ๋ด ์ธ ์ ์๊ฒ ํด ์ฃผ๋ "์ฝ์ผํธ ๋ฐฐ์ ๋ฐ" |
| provider | ์ ๋ณด ํ ์กฐ๊ฐ์ ๋ณด๊ดํ๊ณ ํ์ํ ๋ ๋ด์ด ์ฃผ๋ "์ฝ์ผํธ" ํ๋ |
| ์ฝ๋ ์์ฑ(code generation) | ๋ฐ๋ณต๋๋ ์ฝ๋๋ฅผ ์ฌ๋์ด ์ง์ ์ ์ฐ๊ณ ์๋์ผ๋ก ๋ง๋ค์ด ์ฃผ๋ ๊ธฐ๋ฅ |
| linter (riverpod_lint) | ์ฝ๋์์ ์ค์๋ ์ ์ข์ ์ต๊ด์ ์๋์ผ๋ก ์ง์ด ์ฃผ๋ ๊ฒ์ฌ๊ธฐ |
| Hello world | ์ ๋๊ตฌ๊ฐ ์ ๋์ํ๋์ง ํ์ธํ๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์ฒซ ์์ |
Instructions#
Installing the package#
Riverpod is available as a main package (Flutter: flutter_riverpod; Dart-only: riverpod). Optional packages add code generation and hooks.
Flutter:
flutter pub add flutter_riverpod
With code generation:
flutter pub add flutter_riverpod riverpod_annotation
flutter pub add dev:riverpod_generator build_runner
Dart only:
dart pub add riverpod
With code generation:
dart pub add riverpod riverpod_annotation
dart pub add dev:riverpod_generator build_runner
Manual pubspec.yaml (Flutter):
dependencies:
flutter:
sdk: flutter
flutter_riverpod: ^2.0.0 # use latest from pub.dev
dev_dependencies:
build_runner:
riverpod_generator: ^2.0.0 # if using code generation
Then run flutter pub get (or dart pub get for Dart-only).
If using code generation, run:
dart run build_runner watch -d
Note: Some Riverpod packages may require Flutter's beta channel for compatibility with latest
json_serializable. If pub get fails, try flutter channel beta or use Riverpod
<=3.1.0.
Wrap the app in ProviderScope#
Widgets need a ProviderScope (Flutter) or a ProviderContainer (Dart) to read providers.
Flutter โ ProviderScope:
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
void main() {
runApp(
ProviderScope(
child: MyApp(),
),
);
}
Dart only โ ProviderContainer:
final container = ProviderContainer();
final value = container.read(helloWorldProvider);
print(value);
// Don't forget to container.dispose() when done
Hello world (Flutter with code generation)#
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'main.g.dart';
@riverpod
String helloWorld(Ref ref) {
return 'Hello world';
}
void main() {
runApp(ProviderScope(child: MyApp()));
}
class MyApp extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final String value = ref.watch(helloWorldProvider);
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text('Example')),
body: Center(child: Text(value)),
),
);
}
}
Run dart run build_runner build (or watch) to generate main.g.dart. Without code generation, use a manual provider (e.g.
final helloWorldProvider = Provider<String>((ref) => 'Hello world');) and the same
ProviderScope + ConsumerWidget pattern.
Enabling riverpod_lint#
Add the optional linter via analysis_options.yaml:
plugins:
riverpod_lint: ^2.0.0 # use latest from https://pub.dev/packages/riverpod_lint
This provides lint rules and refactorings for Riverpod. See the riverpod_lint package page for the full list.
Going further#
- Code generation and hooks: See the riverpod-codegen-and-hooks skill (or docs: About code generation, About hooks).
- VS Code / Android Studio: Consider Flutter Riverpod Snippets extensions for faster boilerplate.