rivertion 0.1.0-dev.3 rivertion: ^0.1.0-dev.3 copied to clipboard
Extends riverpod by adding the ability to manipulate data via asynchronous mutations / actions.
Rivertion #
Extends riverpod by adding the ability to manipulate data via asynchronous mutations / actions.
Features #
- Perform asynchronous data mutations
- Refresh a provider's when mutation complete
Usage #
Define your mutation:
final addTodo = MutationProvider((ref) {
return MutationBloc((todo) async {
await Future.delayed(const Duration(seconds: 2));
// Call api and add a todo
_todos = [..._todos, todo];
});
});
Launch your mutation:
final addTodoState = ref.read(addTodo);
ElevatedButton(
onPressed: addTodoState.isMutating
? null
: () => ref.read(addTodo.bloc).mutate("New Todo!"),
child: Text('Add Todo'),
),
Additional information #
Refresh a provider
You can refresh a provider when a mutation ends:
final removeTodo = MutationProvider<String, void>((ref) {
return MutationBloc((todo) async {
await Future.delayed(const Duration(seconds: 2)); // Api call
}, onSuccess: (_, __) async {
await ref.refresh(todos.future);
});
});