riverpod_mutations_generator 2.0.0-dev.0
riverpod_mutations_generator: ^2.0.0-dev.0 copied to clipboard
A code generator for Riverpod Mutations. This covers the hole left in Riverpod today.
example/example.dart
// ignore_for_file: unused_local_variable
import 'package:riverpod/riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:riverpod_mutations_annotation/riverpod_mutations_annotation.dart';
part 'example.g.dart';
class Todo {
const Todo(this.id, this.todo);
final int id;
final String todo;
}
@riverpod
class TodoList extends _$TodoList {
@override
FutureOr<List<Todo>> build() => [];
@mutation
Future<void> addTodo(Todo newTodo) async {
// simulate doing something
await Future.delayed(Duration.zero);
// load the new item into the list
state = AsyncData((await future)..add(newTodo));
// or, do this to let the build method acquire the most up to date information
ref.invalidateSelf();
await future;
}
@mutation
Future<void> removeTodo({@mutationKey required int id}) async {
// simulate doing something
await Future.delayed(Duration.zero);
// remove the item from the list
state = AsyncData((await future)..removeWhere((e) => e.id == id));
// or, do this to let the build method acquire the most up to date information
ref.invalidateSelf();
await future;
}
}
@riverpod
void example(Ref ref) {
// normal usage
final todoList = ref.watch(todoListProvider);
final (addedTodo, addTodo) = ref.watch(todoListProvider.addTodo);
switch (addedTodo) {
case Mut(isLoading: true):
print('Loading...');
case MutIdle():
print('Idle/Initial');
case MutError(:final error):
print(error.toString());
case MutSuccess<void>():
print('Success!');
}
addTodo(Todo(1, 'newTodo'));
final removeTodo = ref.watch(todoListProvider.removeTodo(id: 1));
// the parameter marked by @mutationKey was removed, as it's stored
removeTodo.action();
}