riverpod_mutations_generator 2.0.0-dev.0 copy "riverpod_mutations_generator: ^2.0.0-dev.0" to clipboard
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();
}
6
likes
0
points
34
downloads

Publisher

verified publisheroaka.xyz

Weekly Downloads

A code generator for Riverpod Mutations. This covers the hole left in Riverpod today.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

analyzer, build, build_config, code_builder, collection, meta, recase, riverpod_annotation, riverpod_mutations_annotation, source_gen, source_helper

More

Packages that depend on riverpod_mutations_generator