Loading...
Loading...
Install Riverpod (flutter_riverpod or riverpod), wrap the app in ProviderScope, run a hello-world provider, and optionally enable riverpod_lint and code generation. Use when starting a Flutter or Dart project with Riverpod, adding the Riverpod dependency, or setting up ProviderScope and a first provider. For version highlights see the official Riverpod docs.
npx skill4agent add serverpod/skills-registry riverpod-getting-startedflutter_riverpodriverpodflutter pub add flutter_riverpodflutter pub add flutter_riverpod riverpod_annotation
flutter pub add dev:riverpod_generator build_runnerdart pub add riverpoddart pub add riverpod riverpod_annotation
dart pub add dev:riverpod_generator build_runnerpubspec.yamldependencies:
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 generationflutter pub getdart pub getdart run build_runner watch -djson_serializablepub getflutter channel beta<=3.1.0ProviderScopeProviderContainerimport 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
void main() {
runApp(
ProviderScope(
child: MyApp(),
),
);
}final container = ProviderContainer();
final value = container.read(helloWorldProvider);
print(value);
// Don't forget to container.dispose() when doneimport 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
part 'main.g.dart';
String helloWorld(Ref ref) {
return 'Hello world';
}
void main() {
runApp(ProviderScope(child: MyApp()));
}
class MyApp extends ConsumerWidget {
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)),
),
);
}
}dart run build_runner buildwatchmain.g.dartfinal helloWorldProvider = Provider<String>((ref) => 'Hello world');ProviderScopeConsumerWidgetanalysis_options.yamlplugins:
riverpod_lint: ^2.0.0 # use latest from https://pub.dev/packages/riverpod_lint