几分钟内安装 miniriverpod。
Riverpod 风格的轻量状态管理工具:单文件核心与轻量 Flutter 绑定。
要求
确保项目满足最低 SDK 与 Flutter 版本。
Dart SDK
>=3.10.0 <4.0.0
Flutter
>=3.38.0
提示
如果正在升级项目,请先更新 SDK constraints 再执行安装命令。
安装
使用 Flutter CLI 添加包,或手动更新 pubspec。
CLI
flutter pub add miniriverpod
pubspec.yaml
# Add to your dependencies
dependencies:
miniriverpod: ^0.0.1
入门
接入 ProviderScope,声明 Provider,并在 UI 中 watch。
main.dart
// 1) Wrap your app with ProviderScope
void main() {
runApp(
const ProviderScope(
child: MyApp(),
),
);
}
// 2) Define a Provider
final counterProvider = Provider<int>((ref) => 0);
// 3) Watch from UI
class MyApp extends ConsumerWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final count = ref.watch(counterProvider);
return Directionality(
textDirection: TextDirection.ltr,
child: Center(child: Text('$count')),
);
}
}
Provider((ref) => ...) 是同步的。
AsyncProvider<T>((ref) async => ...) 处理 Future 或 Stream。
在 UI 中使用 WidgetRef.watch(provider) 订阅。
下一步
API Reference
Quickly lookup methods such as invalidate, refreshValue, keepAlive, and invoke.
Open API Reference