數分鐘內安裝 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 監聽。
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