Flutter RIverpod changing state strangely

139 Views Asked by At

I have to state providers. The second one (listWithGlobalIDProvider) depends on the first one (currentListProvider) but the first one is independent. But Adding value to the second provider changes the first provider's value. I am calling the listWithGlobalIDProvider inside a futterProvider.

final currentListProvider =
    StateProvider.autoDispose<BracketList>((ref) {
  final brackets = ref.watch(_currentUserBracketListFutureProvider).value;
  ref.keepAlive();
  return brackets ?? [];
});

final listWithGlobalIDProvider =
    StateProvider.autoDispose<BracketList>((ref) {
  final brackets = ref.watch(currentListProvider);
  final List<String> list = brackets;
  list.add(GlobalBracketID.globalBrackerID);
  ref.keepAlive();
  return list;
});
flutter_riverpod: ^2.2.0

I am expecting that calling the second stateProvider not change the first provider value.

1

There are 1 best solutions below

2
On BEST ANSWER

If I understand your question correctly, you could change the line

final List<String> list = brackets;

to instead be

final List<String> list = List<String>.from(brackets);

This should ensure that modifying the second provider won't change the first provider.