ติดตั้ง miniriverpod ในไม่กี่นาที
เครื่องมือจัดการสถานะแบบ Riverpod ที่เบา มีแกนหลักไฟล์เดียวและบाइน์ดิง Flutter ที่บาง
ข้อกำหนด
ตรวจสอบว่าโปรเจกต์ของคุณตรงกับเวอร์ชันขั้นต่ำของ SDK และ Flutter
Dart SDK
>=3.10.0 <4.0.0
Flutter
>=3.38.0
เคล็ดลับ
หากอัปเกรดโปรเจกต์เดิม ให้ปรับข้อกำหนด SDK ก่อนรันคำสั่งติดตั้ง
การติดตั้ง
เพิ่มแพ็กเกจผ่าน 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 ให้ subscribe ด้วย WidgetRef.watch(provider)
ขั้นตอนถัดไป
API Reference
Quickly lookup methods such as invalidate, refreshValue, keepAlive, and invoke.
Open API Reference