Pasang miniriverpod dalam beberapa minit.
Toolkit pengurusan state gaya Riverpod yang ringan dengan teras satu fail dan ikatan Flutter yang nipis.
Keperluan
Pastikan projek anda memenuhi versi minimum SDK dan Flutter.
Dart SDK
>=3.10.0 <4.0.0
Flutter
>=3.38.0
Tip
Jika anda menaik taraf projek sedia ada, kemas kini kekangan SDK sebelum menjalankan arahan pemasangan.
Pemasangan
Tambah pakej dengan Flutter CLI atau kemas kini pubspec secara manual.
CLI
flutter pub add miniriverpod
pubspec.yaml
# Add to your dependencies
dependencies:
miniriverpod: ^0.0.1
Langkah Pertama
Sambungkan ProviderScope, isytiharkan Provider, dan pantau dari 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) => ...) adalah segerak.
AsyncProvider<T>((ref) async => ...) mengendalikan Future atau Stream.
Dari UI, langgan dengan WidgetRef.watch(provider).
Langkah Seterusnya
API Reference
Quickly lookup methods such as invalidate, refreshValue, keepAlive, and invoke.
Open API Reference