modddels
modddels copied to clipboard
Directly create a case-modddel with preserved type
Freezed supports directly creating a union-case and preserving its type, for example :
@freezed
class MapLayer with _$MapLayer {
const factory MapLayer.raster(int someParam) = Raster;
const factory MapLayer.vector(int someParam) = Vector;
const factory MapLayer.geojson(int someParam) = Geojson;
}
// then, instead of calling `MapLayer.raster(1)` :
final Raster raster = Raster(1);
In modddels, this is not supported (for now). If needed, you can cast the case-modddel directly after creating it, like this :
final raster = MapLayer.raster(
//...
) as Raster;
Although it's pretty safe, it's not very clean so I'll look into implementing a better alternative.
Originally posted by @CodingSoot in https://github.com/CodingSoot/modddels/discussions/8#discussioncomment-6066086