I have a very weird scenario with my Riverpod Providers.
I have two providers, which are a list of a custom object, SiblingModel
.
Here are the providers defined:
final siblingsListProviderSD = StateProvider<List<SiblingModel>>(
(ref) => [],
);
final deleteListSiblingsProviderSD = StateProvider<List<SiblingModel>>(
(ref) => [],
);
At certain point in the app, I am editing the data of one of the providers. Like this:
ref.read(siblingsListProviderSD.notifier).state[index].fields?.name = 'DUMMY NAME';
Now, something very strange is happening, that this value is changing in the list of both of these providers
So for example, after changing it like above, if I Print the result of both providers like this:
print('Upload Item 1 name: ${siblingsListToUpload.first.fields?.name}');
print('Delete Item 1 name: ${siblingsListToDelete.first.fields?.name}');
The output that I get for both is ‘DUMMY NAME’, even though I only changed the value ot eh sublingsListToUpload provider, not the other one.
Why are the values in both providers changing?
Thanks
2
Answers
I don't know why, but the issue was that I wasn't using copyWith.
When I did it like this, it worked:
If I understand correctly, then you can try to track the problem like this:
Only in this case everything works as expected. Do you have a minimal example to reproduce?
It seems to me that your
SiblingModel
object is not so simple. And the problem lies precisely in it.