help: updating nested collections and values
I found the ListCopyWith interface and the CopyWith topic but I'm having a hard time understanding the best approach to reduce this code into something easier to read and write.
appState.copyWith(
timeline: [
for (final e in value.timeline)
if (e.id == timelineMealId)
timelineMeal.copyWith(
meal: timelineMeal.meal.copyWith(
items: [
for (final item in timelineMeal.meal.items)
if (item.id == itemId) x.copyWith(ignore: ignore) else x
],
),
)
],
);
operations
- given String
timelineMealId, StringitemId, and boolignore - update appState.timelineMeal(where id ==
timelineMealId).meal.items(where id ==itemId).ignore =ignore
was hoping for something like
appState.copyWith.timeline.where((e)=>e.id == timelineMealId).items.where((e)=>e.id == itemId).copyWith(ignore: ignore)
The .where of the List opyWith actually filters the elements. Maybe a .select would be cool to select items and then apply changes to only those, without modifying the other items.
You can wrap the .$update method for this.
I've done some experiments. Having a convenient .select(predicate)... method that plays well with nested copy chains requires some large refactoring of the copy implementation.
Maybe I will do it in the future together with a major release, but this will not be done anytime soon. Just fyi.