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,
  );
}
restart ਪੁਰਾਣੇ callers ਨੂੰ CancelledMutation ਸੁੱਟ ਸਕਦਾ ਹੈ।
dropLatest busy ਹੋਣ ਸਮੇਂ DroppedMutation ਸੁੱਟ ਸਕਦਾ ਹੈ।
ਜਦੋਂ ਤੁਸੀਂ ਅਸਥਾਈ cancellation errors ਨੂੰ ਦਬਾਉਣਾ ਚਾਹੁੰਦੇ ਹੋ, UI ਵਿੱਚ ਇਨ੍ਹਾਂ ਨੂੰ ਫੜੋ।

ਅਗਲੇ ਕਦਮ

Flutter API

ConsumerWidget ਜਾਂ ConsumerStatefulWidget ਵਰਤ ਕੇ mutation state ਨੂੰ UI ਨਾਲ ਜੋੜੋ।

Flutter API ਖੋਲ੍ਹੋ

API Reference

invoke, mutation, mutate, ਅਤੇ MutationState types ਨੂੰ ਤੁਰੰਤ review ਕਰੋ।

API Reference ਖੋਲ੍ਹੋ