Mutations
Define mutations as provider methods, monitor state avec MutationToken, et execute through ref.invoke(call).
Blocs de construction des mutations
A mutation is a Call<R, State> returned de 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)
Remarque
Contrairement aux API de type notifier.method(), miniriverpod fait de l'exécution des mutations un objet call de premier ordre.
Exécuter une mutation
Use ref.invoke so cancellation et drop behaviors are surfaced ? the caller.
Exécuter
Options de concurrence
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.
exemple: optimistic update + restart
A common write pattern updates AsyncData optimistically, then syncs avec server response.
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 may throw CancelledMutation ? older callers.
dropLatest peut lever DroppedMutation quand c'est occupé.
Catch these dans UI when you want ? suppress transient cancellation errors.
?tapes suivantes
Flutter API
Bind mutation state ? UI using ConsumerWidget ou ConsumerStatefulWidget.
Ouvrir Flutter API