2

We've been developing a flutter project that uses nested objects, all being marked as @freezed. The overall implementation of the project highly relies on the copyWith function of the freezed package. We're therefore wondering if it is really efficient or not.

Here is an example:

We have an Event object that contains an EventBasicInfo object (as well as other objects) that contains a field description (and others as well).

The state of an event is handled using the BLoC package. To change the description of an event we do something like that:

void _onDescriptionChanged(_DescriptionChanged event, Emitter<EventFormState> emit) {
    emit(
      state.copyWith(
        event: state.event.copyWith(
            basicInfo: state.event.basicInfo
                .copyWith(description: EventDescription(event.descriptionStr)))),
    );
  }

As we can see, this method uses three times the copyWith method only to change one nested field. Is it bad? And is there a better way of doing this?

RominHood
  • 147
  • 2
  • 11

1 Answers1

0

Use frezed, take a look at : https://pub.dev/packages/freezed#going-further-deep-copy

You must define all your objects as @freezed. Then try this (I did not tried to compile):

void _onDescriptionChanged(_DescriptionChanged event, Emitter<EventFormState> emit) {
emit(state.event.copyWith.basicInfo.call(description:   EventDescription(event.descriptionStr)),
  );
}
Mike
  • 335
  • 1
  • 8