如何使用内置值创建嵌套对象

How to create nested object using built value

我想创建一个嵌套对象作为请求发送给 api。非常感谢帮助。

下面是嵌套的内置值class


abstract class BuiltUpdateProfileRequest
    implements
        Built<BuiltUpdateProfileRequest, BuiltUpdateProfileRequestBuilder> {
  // fields go here
  String get firstName;
  String get lastName;
  String get phoneNumber;
  @nullable
  ProfileBilling get billing;

  BuiltUpdateProfileRequest._();

  factory BuiltUpdateProfileRequest(
          [updates(BuiltUpdateProfileRequestBuilder b)]) =
      _$BuiltUpdateProfileRequest;

  static Serializer<BuiltUpdateProfileRequest> get serializer =>
      _$builtUpdateProfileRequestSerializer;
}

abstract class ProfileBilling
    implements Built<ProfileBilling, ProfileBillingBuilder> {
  // fields go here
  @nullable
  String get address1;
  @nullable
  String get address2;
  @nullable
  String get city;
  @nullable
  String get state;
  @nullable
  String get country;
  @nullable
  String get zip;
  ProfileBilling._();

  factory ProfileBilling([updates(ProfileBillingBuilder b)]) = _$ProfileBilling;

  static Serializer<ProfileBilling> get serializer =>
      _$profileBillingSerializer;
}

下面是请求对象,但它在 phone 数字下的计费时抛出错误,说明无法将类型配置文件计费的值分配给 ProfileBillingBuilder 类型的变量。

 final ProfileBilling profileBilling = ProfileBilling((b) => b
      ..address1 = ""
      ..address2 = ""
      ..city = ""
      ..state = ""
      ..country = ""
      ..zip = "");

 final BuiltUpdateProfileRequest builtUpdateProfileRequest =
        BuiltUpdateProfileRequest((b) => b
          ..firstName = firstName
          ..lastName = lastName
          ..phoneNumber = phoneNo
          ..billing = profileBilling);

您需要调用方法 toBuilder() 来创建 ProfileBillingBuilder 变量;

final BuiltUpdateProfileRequest builtUpdateProfileRequest =
    BuiltUpdateProfileRequest((b) => b
      ..firstName = firstName
      ..lastName = lastName
      ..phoneNumber = phoneNo
      ..billing = profileBilling.toBuilder());