Write flow stays explicit.
Define mutations as provider methods, monitor state with MutationToken, and execute through ref.invoke(call).
Mutation Building Blocks
A mutation is a Call<R, State> returned from 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)
Бележка
Unlike notifier.method() style APIs, miniriverpod makes mutation execution a first-class call object.
Execute a mutation
Use ref.invoke so cancellation and drop behaviors are surfaced to the caller.
Run
Concurrency options
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.
Пример: оптимистично обновяване + restart
Често срещан модел на запис първо обновява оптимистично AsyncData, а след това синхронизира със сървърния отговор.
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 може да хвърли CancelledMutation към по-стари извиквания.
dropLatest може да хвърли DroppedMutation, докато е заето.
Хващайте ги в UI, когато искате да потиснете временните грешки от отказ.
Next Steps
Flutter API
Bind mutation state to UI using ConsumerWidget or ConsumerStatefulWidget.
Open Flutter API