Tra cứu API nhanh cho công việc hằng ngày.
Trang này tóm tắt các lớp và phương thức thường dùng từ miniriverpod_core.dart và miniriverpod_flutter.dart.
Các kiểu cốt lõi
Các trạng thái cốt lõi và các primitive của provider.
AsyncValue<T>
AsyncLoading / AsyncData / AsyncError
Provider<T>
Provider đồng bộ với autoDispose tùy chọn
AsyncProvider<T>
Provider bất đồng bộ với bộ chọn .future
Mẹo đọc
AsyncValue không có phương thức when; hãy dùng khớp mẫu của switch hoặc các kiểm tra is.
Các phương thức của Ref và ProviderContainer
Các phương thức thao tác phổ biến cho refresh, invalidation, lifecycle và thực thi mutation.
Trình tự phổ biến
Sơ đồ phương thức
read / watch / listen : đọc và đăng ký theo dõi provider.
invalidate / refresh / refreshValue : tính toán lại trạng thái.
onDispose / keepAlive / emit : vòng đời và kết nối stream.
mutation / mutate / invoke : các thao tác ghi được theo dõi.
scope / overrideWithValue : DI và override cho kiểm thử.
Ảnh chụp nhanh API Flutter
Các điểm vào ProviderScope + WidgetRef được dùng trong mã ứng dụng.
// Phạm vi
ProviderScope(
child: const App(),
);
// ConsumerWidget
class Header extends ConsumerWidget {
const Header({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final user = ref.watch(currentUser);
return Text('$user');
}
}
// Thực thi mutation
await ref.invoke(userProvider.rename('Alice'));
ProviderScope(container: external) yêu cầu gọi container.dispose() thủ công.
UncontrolledProviderScope không bao giờ dispose container được truyền vào.
Consumer, ConsumerWidget và ConsumerStatefulWidget đều được hỗ trợ.