पढ़ने के रास्ते: watch, read और listen।
Provider सिंक्रोनस state देता है। AsyncProvider AsyncValue लौटाता है और सख्त lifecycle handling के साथ Future और Stream पेश कर सकता है।
Provider प्रकार
डेटा स्रोत और update frequency के आधार पर provider type चुनें।
Provider<T>
सिंक्रोनस values और local derived state
AsyncProvider<T>
Future/Stream-चालित state के रूप में AsyncValue<T>
provider.future
ऐसा selector जो Provider<Future<T>> expose करता है
संकेत
यदि state network या stream से आती है, तो पहले AsyncProvider चुनें। Provider को pure synchronous logic के लिए रखें।
पढ़ने के API की मैट्रिक्स
build paths में watch, one-shot access के लिए read, और side effects के लिए listen का उपयोग करें।
सबसे आम
watch / read / listen
ref.watch(provider) : subscribe and rebuild when value changes.
ref.read(provider) : read current snapshot without subscribing.
container.listen(...) : callback-driven updates; optional fireImmediately.
AsyncProvider के साथ Future और Stream
एक AsyncProvider Future लौटा सकता है या ref.emit(stream) से Stream जोड़ सकता है।
final currentUser = AsyncProvider<User>((ref) async {
final api = ref.watch(apiProvider);
return api.me();
});
final liveUser = AsyncProvider<User>((ref) {
final stream = ref.watch(apiProvider).live();
ref.emit(stream);
return const User(name: 'Loading...');
}, autoDispose: true, autoDisposeDelay: const Duration(milliseconds: 250));
// Await as Future
final user = await ref.watch(currentUser.future);
अगले कदम
Mutations
write operations को provider methods में ले जाएँ और उन्हें ref.invoke से चलाएँ।
Flutter API खोलें