My statenotifier does not rebuild when the map changes. I think the issue is that the map is inside another class. I use riverpod and i've seen it work with lists so I tested with a List inside a class and filter rebuilds to that class. But no success
StateNotifier using copyWith to replace a map does not trigger rebuild
class Admin {
const Admin({
this.admin = const {},
});
final Map<int, List<String>> admin;
Admin copyWith({
Map<int, List<String>>? admin,
}) {
return Admin(
admin: admin ?? this.admin,
);
}
}
class Member {
const Member({
this.wordOfTheDay = "",
this.admin = const Admin(),
});
final Admin admin;
final String wordOfTheDay;
Member copyWith({
Admin? admin,
String? wordOfTheDay,
}) {
return Member(
admin: admin ?? this.admin,
wordOfTheDay: wordOfTheDay ?? this.wordOfTheDay,
);
}
}
class MemberNotifier extends StateNotifier<AsyncValue<Member>> {
MemberNotifier() : super(const AsyncValue.loading());
final Map<int, List<String>> admins = {};
void loadMembers(String id, String item) {
admins[id]!.add(item);
state = state.whenData((value) => value.copyWith(admin: state.value!.admin.copyWith(admin: admins)));
}
}
final watch = ref.watch(memberNotifierProvider.select((member) => member.value!.admin));