I have a list of todo elements managed by:
@Riverpod(keepAlive: true)
class TodosNotifier extends _$TodosNotifier {
@override
List<Todo> build() {
return [.....some elements here.....];
}
void addTodo(Map<String, Object?> todo) {
Todo todo = Todo.fromJson(todo);
state = [...state, todo];
}
...other methods here that change the state for different reasons...
}
and I have another Notifier
which is meant to change its value only when the size of the TodosNotifier
list changes (via addTodo(...)
).
@Riverpod(keepAlive: true)
class TodosLengthNotifier extends _$TodosLengthNotifier {
@override
int build() => 0;
void update() {
// it never gets called here
List<Todo> todos = ref.watch(todosNotifierProvider);
if (state != todos.length) {
state = todos.length;
}
}
}
The problem is that the method TodosLengthNotifier.update()
never gets called every time I add a new “todo” element with TodosNotifier.addTodo()
. How can I fix that?