Write flow ਸਪਸ਼ਟ ਰਹਿੰਦਾ ਹੈ।
Mutations ਨੂੰ provider methods ਵਜੋਂ define ਕਰੋ, MutationToken ਨਾਲ state monitor ਕਰੋ, ਅਤੇ ref.invoke(call) ਰਾਹੀਂ execute ਕਰੋ।
Mutation ਦੇ ਬਿਲਡਿੰਗ ਬਲਾਕ
ਇੱਕ mutation mutate(token, body, concurrency: ...) ਤੋਂ ਵਾਪਸ ਆਇਆ Call<R, State> ਹੈ।
Token
late final renameMut = mutation<void>(#rename)
Execution
await ref.invoke(provider.rename(...))
State
Idle / Pending / Success / Error via ref.watch(token)
ਨੋਟ
notifier.method() style APIs ਤੋਂ ਵੱਖਰੇ, miniriverpod mutation execution ਨੂੰ ਪਹਿਲੀ ਦਰਜੇ ਦੇ call object ਵਿੱਚ ਬਦਲਦਾ ਹੈ।
ਇੱਕ mutation execute ਕਰੋ
ref.invoke ਵਰਤੋਂ ਤਾਂ ਜੋ cancellation ਅਤੇ drop behaviors caller ਨੂੰ ਦਿਖਾਈ ਦੇਣ।
ਲੰਬਾਈ (Run)
Concurrency ਵਿਕਲਪ
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 ਤਰੀਕੇ ਨਾਲ update ਕਰਦਾ ਹੈ, ਫਿਰ 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,
);
}
ਅਗਲੇ ਕਦਮ
Flutter API
ConsumerWidget ਜਾਂ ConsumerStatefulWidget ਵਰਤ ਕੇ mutation state ਨੂੰ UI ਨਾਲ ਜੋੜੋ।
Flutter API ਖੋਲ੍ਹੋAPI Reference
invoke, mutation, mutate, ਅਤੇ MutationState types ਨੂੰ ਤੁਰੰਤ review ਕਰੋ।
API Reference ਖੋਲ੍ਹੋ