Cài miniriverpod trong vài phút.
Bộ công cụ state management kiểu Riverpod với core một file và binding Flutter mỏng.
Yêu cầu
Đảm bảo dự án đáp ứng phiên bản SDK và Flutter tối thiểu.
Dart SDK
>=3.10.0 <4.0.0
Flutter
>=3.38.0
Mẹo
Nếu đang nâng cấp dự án, hãy cập nhật SDK constraints trước khi chạy lệnh cài đặt.
Cài đặt
Thêm package bằng Flutter CLI hoặc cập nhật pubspec thủ công.
CLI
flutter pub add miniriverpod
pubspec.yaml
# Add to your dependencies
dependencies:
miniriverpod: ^0.0.1
Bước đầu
Kết nối ProviderScope, khai báo Provider và watch từ 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) => ...) là đồng bộ.
AsyncProvider<T>((ref) async => ...) xử lý Future hoặc Stream.
Từ UI, subscribe bằng WidgetRef.watch(provider).
Bước tiếp theo
API Reference
Quickly lookup methods such as invalidate, refreshValue, keepAlive, and invoke.
Open API Reference