write flow explicit बना रहता है.
mutations को provider methods के रूप में define करें, MutationToken के साथ state monitor करें, और उन्हें ref.invoke(call) के माध्यम से execute करें.
Mutations के निर्माण खंड
एक mutation एक Call<R, State> है जो 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)
नोट
notifier.method() शैली के APIs के विपरीत, miniriverpod mutation execution को एक first-class call object बनाता है.
mutation चलाएँ
ref.invoke का उपयोग करें ताकि cancellation और drop behavior 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 को optimistically 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 से bind करें.
Flutter API खोलें