Update a BuiltList from built_value

571 views Asked by At

I want to update the BuiltList in the state if some condition is met. Here is my reducer

TravelDeductionsStateBuilder _travelDeductionBreakfastToggled(
        TravelDeductionsStateBuilder state,
        TravelDeductionBreakfastToggledAction action) =>
    state
      ..days = state.days.build().rebuild((d) {
        if (d[action.key].deductions.contains(TravelDeductionType.BREAKFAST)) {
          d[action.key]
              .deductions
              .rebuild((p0) => p0.remove(TravelDeductionType.BREAKFAST));
        } else {
          d[action.key]
              .deductions
              .rebuild((p0) => p0.add(TravelDeductionType.BREAKFAST));
        }
      }).toBuilder();

I need to add/remove ENUM entry into deductions but it seems that this update is ignored and I am not able to update this list accordingly. Any tips of what may be wrong?

1

There are 1 answers

0
kmtz On BEST ANSWER

If anyone wonders this is the solution

TravelDeductionsStateBuilder _travelDeductionBreakfastToggled(
        TravelDeductionsStateBuilder state,
        TravelDeductionBreakfastToggledAction action) =>
    state
      ..days = state.days.build().rebuild((d) {
        if (d[action.index]
            .deductions
            .contains(TravelDeductionType.BREAKFAST)) {
          d[action.index] = d[action.index].rebuild(
              (b) => b.deductions.remove(TravelDeductionType.BREAKFAST));
        } else {
          d[action.index] = d[action.index]
              .rebuild((b) => b.deductions.add(TravelDeductionType.BREAKFAST));
        }
      }).toBuilder();

You can find the explanation here https://github.com/google/built_collection.dart/issues/259#issuecomment-1122390601