dart_mappable icon indicating copy to clipboard operation
dart_mappable copied to clipboard

help: updating nested collections and values

Open lukepighetti opened this issue 1 year ago • 2 comments

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

  1. given String timelineMealId, String itemId, and bool ignore
  2. 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)

lukepighetti avatar Sep 10 '24 13:09 lukepighetti

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.

schultek avatar Sep 11 '24 11:09 schultek

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.

schultek avatar Sep 25 '24 09:09 schultek