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

Flutter - Freezed package - How to properly compose classes

我很难理解如何将包用于基本案例,例如描述 API requests/responses。我可能陷入了一个循环,在这个循环中我想到了一些事情,我正试图不惜一切代价让它以这种方式工作,但我看不到更简单的解决方案。

示例:

@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;
}

然后

@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);
}

那里有些东西很臭,我确定我遗漏了一些东西而且我无法正确组合这些东西 类。 在这种情况下,冻结甚至可能是矫枉过正?

你不能implement/extend冻结类。

要么将您的 BaseResponse/BaseRequest 更改为:

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

abstract class BaseResponse{
  bool get c;
}

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

@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;
}