INDEX / JOURNAL / LOG-020
VOL. 2026 · ENGINEERING
LOG-020 · RIVERPOD CODEGEN

Riverpod のコード生成が書くもの

PUBLISHED
2026 · 10 · 23
AUTHOR
MIGIWAYA
READ
約 6 分
TAGS
#flutter#dart#riverpod

§ 01SHAPE

lib/providers/current_user_provider.dartDART
final currentUserProvider = FutureProvider<User>(
(ref) async => fetchCurrentUser(),
isAutoDispose: true,
);
lib/providers/current_user.dartDART
@riverpod
Future<User> currentUser(Ref ref) async {
return fetchCurrentUser();
}
lib/providers/counter_provider.dartDART
final counterProvider =
NotifierProvider<Counter, int>(Counter.new);
 
class Counter extends Notifier<int> {
@override
int build() => 0;
 
void increment() => state++;
}
lib/providers/counter.dartDART
@riverpod
class Counter extends _$Counter {
@override
int build() => 0;
 
void increment() => state++;
}
注釈を付けるもの戻り値・状態生成される入口
StringProvider<String> に対応する変数
Future<User>FutureProvider<User> に対応する変数
intNotifierProvider<Counter, int> に対応する変数

§ 02PACKAGES

pubspec.yamlYAML
dependencies:
flutter_riverpod:
riverpod_annotation:
 
dev_dependencies:
build_runner:
riverpod_generator:

§ 03DISPOSE

lib/providers/session.dartDART
@riverpod
Session session(Ref ref) => loadSession();
lib/providers/app_config.dartDART
@Riverpod(keepAlive: true)
AppConfig appConfig(Ref ref) => loadConfig();

§ 04FAMILY

lib/providers/product.dartDART
@riverpod
Future<Product> product(
Ref ref,
String id, {
String locale = 'ja',
bool includeStock = false,
}) async {
return fetchProduct(
id,
locale: locale,
includeStock: includeStock,
);
}
lib/widgets/product_page.dartDART
final product = ref.watch(
productProvider(
productId,
locale: 'ja',
includeStock: true,
),
);

§ 05RIVERPOD-3

DART
@riverpod
String greeting(GreetingRef ref) => 'Hello';
DART
@riverpod
String greeting(Ref ref) => 'Hello';

§ 06CHOICE

§ 07SUMMARY

脚注 · NOTES
  1. [1] ↩
  2. [2] ↩
  3. [3] ↩
§ + RELATED すべて見る →
INQUIRY
CONTACT
LOCATION
STATUS
Active / Open
ESTABLISHED
2024 · 5 · 5
FORM