River_pod: In development, make StateProvider wrap collections in an UnmodifiableListView

Created on 5 Sep 2020  路  2Comments  路  Source: rrousselGit/river_pod

Describe the bug
When I define a StateProvider as a list, an add or remove doesn't trigger the watch.

To Reproduce
final goalStates = StateProvider( (_) => [ GoalState.ACTIVE, ], );

final _states = watch(goalStates);

onPressed: () { _states.state.add(GoalState.COMPLETE); _states.state = _states.state; },

It doesn't trigger on the .add line, but it does on the = line

Expected behavior
Shouldn't it also trigger on the add?

enhancement

Most helpful comment

This is indeed expected

On the other hand I wonder if during development StateProvider should detect that the value is a list and wrap it in an UnmodifiableListView

This would throw an error instead of failing silently

All 2 comments

@arnoutvandervorst That is expected. The state instance is the same even though you add elements to the list. You are just mutating the list.
To detect changes you should make new instances of your list.

_states.state = _states.state.toList()..add(...)

This is indeed expected

On the other hand I wonder if during development StateProvider should detect that the value is a list and wrap it in an UnmodifiableListView

This would throw an error instead of failing silently

Was this page helpful?
0 / 5 - 0 ratings