bound_mutation 2.1.0
bound_mutation: ^2.1.0 copied to clipboard
A small wrapper class around Riverpod Mutations to bind a function to a mutation.
bound_mutation #
A wrapper around riverpod's Mutation that binds an input parameter to the mutation callback. Instead of capturing input at construction time, you pass it when calling run.
Features #
BoundMutation<ResultT, InputR>— mutation with an input parameter passed torunBoundAction<ResultT>— mutation without input- Full
ProviderListenable<MutationState<ResultT>>integration — listen to idle/pending/success/error states - Thin wrapper: delegates directly to
Mutation.runandMutation.reset
Usage #
import 'package:bound_mutation/bound_mutation.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
// Define a bound mutation: ResultT = String, InputR = int
final createUser = BoundMutation<String, int>(
(transaction, userId) async {
// transaction gives access to providers
final repo = transaction.get(userRepositoryProvider);
return repo.fetchUserName(userId);
},
);
// Inside a provider or widget with a Ref:
Future<void> loadUser(WidgetRef ref) async {
// Watch mutation state
final state = ref.watch(createUser);
if (state.isIdle) {
final name = await createUser.run(ref, 42);
print('User: $name');
} else if (state.isPending) {
print('Loading...');
} else if (state.isSuccess) {
print('Success: ${(state as MutationSuccess<String>).value}');
} else if (state.hasError) {
final err = state as MutationError<String>;
print('Error: ${err.error}');
}
}
// Reset mutation state (back to idle):
createUser.reset(ref);
With ProviderContainer (e.g. in tests) #
final container = ProviderContainer();
final result = await createUser.run(container, 42);
createUser.reset(container);
container.dispose();
BoundAction<ResultT> — mutation without input #
final refreshFeed = BoundAction<void>((transaction) async {
final repo = transaction.get(feedRepositoryProvider);
await repo.refresh();
});
await refreshFeed.run(ref);
API #
| Method | Description |
|---|---|
BoundMutation(cb, {label}) |
Creates a mutation with a callback (transaction, input) -> Future<ResultT> |
BoundMutation.run(target, input) |
Executes the mutation, returning Future<ResultT> |
BoundAction(cb, {label}) |
Creates a mutation without input, callback (transaction) -> Future<ResultT> |
BoundAction.run(target) |
Executes the mutation, returning Future<ResultT> |
reset(target) |
Resets the mutation state back to MutationIdle |
source |
Returns the underlying Mutation<ResultT> |
== / hashCode |
Delegates to the internal Mutation |
BoundMutation and BoundAction implement ProviderListenable<MutationState<ResultT>>, so they can be watched via ref.watch() or container.listen().