Flutter - 冻结的包 - 如何正确组合类

Gio*_*ero 2 composition dart flutter

我很难理解如何使用该包来处理基本情况,例如描述 API 请求/响应。我可能会陷入一个循环,我心里已经有了一些想法,我正试图不惜一切代价让它发挥作用,但我看不到更简单的解决方案。

例子:

@freezed
abstract class BaseRequest with _$BaseRequest {
  const factory BaseRequest({
    @required int a,
    @required String b,
  }) = _BaseRequest;
}

@freezed
abstract class BaseResponse with _$BaseResponse {
  const factory BaseResponse({
    @required bool c,
  }) = _BaseResponse;
}
Run Code Online (Sandbox Code Playgroud)

然后

@freezed
abstract class Authentication with _$Authentication {
  @Implements(BaseRequest)
  const factory Authentication.request({
    @required int a,
    @required String b,
    @required String psw,
  }) = _AuthenticationRequest;

  @Implements(BaseResponse)
  const factory Authentication.response({
    @required bool c,
    @required String token,
  }) = _AuthenticationResponse;

  factory Authentication.fromJson(Map<String, dynamic> json) =>
      _$AuthenticationFromJson(json);
}
Run Code Online (Sandbox Code Playgroud)

那里有一些臭味,我确信我遗漏了一些东西,而且我无法正确地编写这些课程。在这种情况下,“冻结”是否可能有点过分了?

Rém*_*let 8

您无法实现/扩展 Freezed 类。

BaseResponse/更改BaseRequest为:

abstract class BaseRequest {
  int get a;
  String get b;
}

abstract class BaseResponse{
  bool get c;
}
Run Code Online (Sandbox Code Playgroud)

或者使用组合而不是继承:

@freezed
abstract class Authentication with _$Authentication {
  const factory Authentication.request({
    @required BaseRequest request,
    @required String psw,
  }) = _AuthenticationRequest;

  const factory Authentication.response({
    @required BaseResponse response,
    @required String token,
  }) = _AuthenticationResponse;
}
Run Code Online (Sandbox Code Playgroud)