'Freezed union with inline existing class

I have the following JSON

{
  "items": [ 
     {
         "type": "text",
         "text": "some text"
     },
     {
         "type": "image",
         "url": "https://some.url/some/path",
         "width": 400
     },
  ]
}

I have modelled this with Freezed as below

class Feed with _$Feed {
  const factory Feed({required List<BaseItem> items}) = _Feed;
  ...
}

@Freezed(unionKey: 'type')
class BaseItem with _$BaseItem {
  @FreezedUnionValue('text')
  const factory BaseItem.Text() = TextItem;
  @FreezedUnionValue('image')
  const factory BaseItem.Image({required String imageUrl, required int width}) =
      _ImageItem;
  ...
}

I have another JSON that re uses the BaseItem.Image model, and I would like to define it only once and re use it in code as well.

I can do something like below (as suggested here)

const factory BaseItem.Image({required ImageItem item}) = _ImageItem;

where ImageItem is the class defining this shared structure.

But this forces the first JSON to have this new item property and to nest the ImageItem under it.

Is there a way to share the ImageItem structure AND not have a separate property in first JSON?

something like

const factory BaseItem.Image({required ImageItem _}) = _ImageItem;
// where _ means inline the ImageItem proeprties here ?


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source