Write flow واضح رہتا ہے۔
Mutations کو provider methods کے طور پر define کریں، MutationToken سے state monitor کریں، اور ref.invoke(call) کے ذریعے execute کریں۔
Mutation کے بنیادی اجزاء
ایک mutation ایک Call<R, State> ہے جو mutate(token, body, concurrency: ...) سے واپس آتا ہے۔
Token
late final renameMut = mutation<void>(#rename)
Execution
await ref.invoke(provider.rename(...))
State
Idle / Pending / Success / Error via ref.watch(token)
نوٹ
notifier.method() طرز کی APIs کے برعکس، miniriverpod mutation execution کو first-class call object بناتا ہے۔
Mutation چلائیں
ref.invoke استعمال کریں تاکہ cancellation اور drop behaviors caller کو نظر آئیں۔
چلائیں
مقابلتی اختیارات
concurrent : run all calls in parallel (default).
queue : FIFO; keep running queued calls even after an error.
restart : cancel previous run, keep only latest call.
dropLatest : drop incoming calls while one is running.
مثال: optimistic update + restart
ایک عام write pattern AsyncData کو optimistic طور پر اپ ڈیٹ کرتا ہے، پھر server response کے ساتھ sync ہوتا ہے۔
class UserProvider extends AsyncProvider<User?> {
UserProvider() : super.args(null);
late final renameMut = mutation<void>(#rename);
Call<void, AsyncValue<User?>> rename(String newName) => mutate(
renameMut,
(ref) async {
final cur = ref.watch(this).valueOrNull;
ref.state = AsyncData((cur ?? const User()).copyWith(name: newName), isRefreshing: true);
final api = ref.watch(apiProvider);
await api.rename(newName);
ref.state = AsyncData(await api.me());
},
concurrency: Concurrency.restart,
);
}
aglay marahil
Flutter API
ConsumerWidget یا ConsumerStatefulWidget استعمال کرکے mutation state کو UI سے جوڑیں۔
Flutter API کھولیںAPI Reference
invoke, mutation, mutate, اور MutationState types کو جلدی review کریں۔
API Reference کھولیں