Skip to content

Persist child provider not reacting to persisted data #4802

Description

@robpot95

Describe the bug
Watching sortedTodosdosProvider using await .future is not showing persisted data. While watching todosProvidereverything works fine

To Reproduce

@riverpod
Future<List<Todo>> sortedTodos(Ref ref) async {
  final todos = await ref.watch(todosProvider.future);
  return todos..sort((a, b) => a.name.compareTo(b.name));
}

@riverpod
@JsonPersist()
class Todos extends _$Todos {
  @override
  FutureOr<List<Todo>> build() async {
    persist(
      ref.watch(riverpodCacheProvider.future),
      key: "todos_list",
    );

    if (Random().nextBool()) {
      await Future.delayed(const Duration(seconds: 1), () => throw Exception("Network error"));
    }

    return Future.delayed(const Duration(seconds: 3), () {
      return <Todo>[
        const Todo(id: 1, name: "Wash car"),
        const Todo(id: 2, name: "Cook"),
      ];
    });
  }
}

What works if we remove await .future:

@riverpod
Future<List<Todo>> sortedTodos(Ref ref) async {
  final todos = ref.watch(todosProvider).value ?? [];
  return todos..sort((a, b) => a.name.compareTo(b.name));
}

Expected behavior
Should show persisted data if parent has data.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingquestionFurther information is requested

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions