9

I am using the freezed package to create my json parsers and data classes. As of now I am using dartz's package to create Union cases and handle them in Flutter widgets. For example some of my Union classes looks like the following.

Either<ApiFailure,ModelA> apiResult1;
Either<ApiFailure,ModelB> apiResult2;

and I use them in my flutter widget's builder by folding them and returning specific widget for each cases. For example.

return apiResul1.fold<Widget>(
  (left) => ErrorWidget(),
  (right) => SuccessWidget(),
);

I created ApiFailure, ModelA, and ModelB as data classes using the freezed package. I understood freezed comes with a similar Union class support like dartz where we can define union cases. So I tried using them as follows and based on my initial understanding it is not possible to achieve the following using the already existing data classes for eg. ModelA andApiFailure`.

@freezed
abstract class ApiResult1 with _$ApiResult1{
  const factory ApiResult1.modelA() =  ModelA;
  const factory ApiResult1.apiFailure() =  ApiFailure;
}

@freezed
abstract class ApiResult2 with _$ApiResult1{
  const factory ApiResult2.modelB() =  ModelB;
  const factory ApiResult2.apiFailure() =  ApiFailure;
}

Notice that in the above two union classes I am redefining ApiFailure in ApiResult2 which is what I am trying to Avoid.

Question: Is it possible to utilize existing dataclasses to build a union class so that I don't have to make multiple changes just to change the structure of ApiFailure object. Hope my question is clear.

Abhilash Chandran
  • 6,803
  • 3
  • 32
  • 50
  • I'm facing the same issue. My usecase is the following: I have two freezed classes representing failures: "Feature1Failure", "Feature2Failure". I would like to create a FeaturesFailure union class reusing the two existing one. – Fabrizio Cacicia Jan 07 '21 at 18:22
  • 1
    Sorry I got a reply in twitter. According to the [tweet](https://twitter.com/scabhilash/status/1289331998381584384?s=20) from @remi this is not possible with freezed as of now. – Abhilash Chandran Jan 08 '21 at 08:47

1 Answers1

0

Why not use your data classes as parameters?

@freezed
abstract class ApiResult1 with _$ApiResult1 {
  const factory ApiResult1.model(ModelA model) = _ModelA;
  const factory ApiResult1.failure(ApiFailure failure) = _ApiFailure1;
}

@freezed
abstract class ApiResult2 with _$ApiResult2 {
  const factory ApiResult2.model(ModelB model) = _ModelB;
  const factory ApiResult2.failure(ApiFailure failure) = _ApiFailure2;
}

Then you can do

  return apiResult1.when(
    model: (model) => SuccessWidget(),
    failure: (failure) => ErrorWidget(),
  );
Azbuky
  • 1,714
  • 1
  • 12
  • 6