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() शैलीतील APIs च्या विरुद्ध, miniriverpod mutation execution ला first-class call object बनवते.

एक mutation चालवा

ref.invoke वापरा, जेणेकरून cancellation आणि drop वर्तन caller ला दिसेल.

चालवा

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 शी bind करा.

Flutter API उघडा

API Reference

invoke, mutation, mutate, आणि MutationState types पटकन पहा.

API Reference उघडा