1

I have this union:

@freezed
abstract class VeganItem extends VeganItemAbstraction with _$VeganItem {
  @With(GroceryItemMixin)
  const factory VeganItem.groceryItem(
      {int? id,
      String? name,
      String? companyName,
      String? description,
      List<Option>? tags,
      int? rating,
      List<ImagePickerImage>? images}) = GroceryItem;..........

There are 4 other subtypes in VeganItem.

I have this union type which contains a VeganItem:

@freezed
abstract class VeganItemEstablishment extends VeganItemEstablishmentAbstraction
    with _$VeganItemEstablishment {
  @With(GroceryItemEstablishmentMixin)
  const factory VeganItemEstablishment.groceryItemEstablishment(
      {int? id,
      Establishment? establishment,
      GroceryItem? veganItem,
      double? price,}) = GroceryItemEstablishment;..............

When I make GroceryItemEstablishment.veganItem be type VeganItem, I can use copywith like this:

  mvipVm.entity =
      mvipVm.entity!.copyWith.veganItem!(id: null, images: null);

But when I make it be type GroceryItem, I have to use copywith like this:

  mvipVm.entity = mvipVm.entity!.map(
    groceryItemEstablishment: (value) => value.copyWith(
        veganItem: mvipVm.entity!.veganItem!
            .copyWith(id: null, images: null) as GroceryItem),
    menuItemEstablishment: (value) => value.copyWith(
        veganItem: mvipVm.entity!.veganItem!
            .copyWith(id: null, images: null) as MenuItem),
    restaurantItemEstablishment: (value) => value.copyWith(
        veganItem: mvipVm.entity!.veganItem!
            .copyWith(id: null, images: null) as RestaurantItem),
    eventItemEstablishment: (value) => value.copyWith(
        veganItem: mvipVm.entity!.veganItem!
            .copyWith(id: null, images: null) as EventItem),
    groceryStoreItemEstablishment: (value) => value.copyWith(
        veganItem: mvipVm.entity!.veganItem!
            .copyWith(id: null, images: null) as GroceryStoreItem),
  );

The docs mention the field must be on all subtypes to be in copyWith. But what about having the same field but with different subtypes?

Is there an alternative solution where I can enter a subtype into the GroceryItemEstablishment.veganItem field that gives me a more concise copyWith syntax?

EDIT: So far the alternative, keep veganItem as the supertype (VeganItem), and just type cast it to the subtype (GroceryItem) when necessary, is way more manageable.

BeniaminoBaggins
  • 11,202
  • 41
  • 152
  • 287

0 Answers0