Dart - Flutter,即使设置了值,字段值仍保持为空

8zzbczxx  于 2023-04-27  发布在  Flutter
关注(0)|答案(1)|浏览(129)

我有一个自定义对象,我像这样示例化:

MatchModel newMatchModel = MatchModel();

然后我设置一个属性如下:

newMatchModel.fields?.maleFirstName =['33'];

但是这个字段的值仍然为null,并且没有被设置为我想要的值。
下面是模型类:

class MatchModel {

  MatchModel({
    this.id,
    this.createdTime,
    this.fields,
  });

  String? id;
  DateTime? createdTime;
  Fields? fields;

  MatchModel copyWith({
    String? id,
    DateTime? createdTime,
    Fields? fields,
  }) =>
      MatchModel(
        id: id ?? this.id,
        createdTime: createdTime ?? this.createdTime,
        fields: fields ?? this.fields,
      );

  factory MatchModel.fromJson(Map<String, dynamic> json) => MatchModel(
    id: json["id"],
    createdTime: json["createdTime"] == null ? null : DateTime.parse(json["createdTime"]),
    fields: json["fields"] == null ? null : Fields.fromJson(json["fields"]),
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "createdTime": createdTime?.toIso8601String(),
    "fields": fields?.toJson(),
  };
}
class Fields {
  Fields({
    this.female,
    this.id,
    this.male,
    this.created,
    this.lastModified,
    this.maleFirstName,
    this.maleLastName,
    this.femaleFirstName,
    this.femaleLastName,
    this.notes,
    this.status,
    this.shadchan,
    this.numberOfDates,
  });

  List<String>? female;
  int? id;
  List<String>? male;
  DateTime? created;
  DateTime? lastModified;
  List<String>? maleFirstName;
  List<String>? maleLastName;
  List<String>? femaleFirstName;
  List<String>? femaleLastName;
  String? notes;
  String? status;
  List<String>? shadchan;
  int? numberOfDates;

  Fields copyWith({
    List<String>? female,
    int? id,
    List<String>? male,
    DateTime? created,
    DateTime? lastModified,
    List<String>? maleFirstName,
    List<String>? maleLastName,
    List<String>? femaleFirstName,
    List<String>? femaleLastName,
    String? notes,
    String? status,
    List<String>? shadchan,
    int? numberOfDates,
  }) =>
      Fields(
        female: female ?? this.female,
        id: id ?? this.id,
        male: male ?? this.male,
        created: created ?? this.created,
        lastModified: lastModified ?? this.lastModified,
        maleFirstName: maleFirstName ?? this.maleFirstName,
        maleLastName: maleLastName ?? this.maleLastName,
        femaleFirstName: femaleFirstName ?? this.femaleFirstName,
        femaleLastName: femaleLastName ?? this.femaleLastName,
        notes: notes ?? this.notes,
        status: status ?? this.status,
        shadchan: shadchan ?? this.shadchan,
        numberOfDates: numberOfDates ?? this.numberOfDates,
      );

  factory Fields.fromJson(Map<String, dynamic> json) => Fields(
    female: json["Female"] == null ? [] : List<String>.from(json["Female"]!.map((x) => x)),
    id: json["ID"],
    male: json["Male"] == null ? [] : List<String>.from(json["Male"]!.map((x) => x)),
    created: json["Created"] == null ? null : DateTime.parse(json["Created"]),
    lastModified: json["Last Modified"] == null ? null : DateTime.parse(json["Last Modified"]),
    maleFirstName: json["Male First Name"] == null ? [] : List<String>.from(json["Male First Name"]!.map((x) => x)),
    maleLastName: json["Male Last Name"] == null ? [] : List<String>.from(json["Male Last Name"]!.map((x) => x)),
    femaleFirstName: json["Female First Name"] == null ? [] : List<String>.from(json["Female First Name"]!.map((x) => x)),
    femaleLastName: json["Female Last Name"] == null ? [] : List<String>.from(json["Female Last Name"]!.map((x) => x)),
    notes: json["Notes"],
    status: json["Status"],
    shadchan: json["Shadchan"] == null ? [] : List<String>.from(json["Shadchan"]!.map((x) => x)),
    numberOfDates: json["Number of dates"],
  );

  Map<String, dynamic> toJson() => {
    "Female": female == null ? [] : List<dynamic>.from(female!.map((x) => x)),
    "ID": id,
    "Male": male == null ? [] : List<dynamic>.from(male!.map((x) => x)),
    "Created": created?.toIso8601String(),
    "Last Modified": lastModified?.toIso8601String(),
    "Male First Name": maleFirstName == null ? [] : List<dynamic>.from(maleFirstName!.map((x) => x)),
    "Male Last Name": maleLastName == null ? [] : List<dynamic>.from(maleLastName!.map((x) => x)),
    "Female First Name": femaleFirstName == null ? [] : List<dynamic>.from(femaleFirstName!.map((x) => x)),
    "Female Last Name": femaleLastName == null ? [] : List<dynamic>.from(femaleLastName!.map((x) => x)),
    "Notes": notes,
    "Status": status,
    "Shadchan": shadchan == null ? [] : List<dynamic>.from(shadchan!.map((x) => x)),
    "Number of dates": numberOfDates,
  };
}

为什么字段没有设置?
谢谢!

mbskvtky

mbskvtky1#

在设置maleFirstName属性之前,需要示例化fields对象:

MatchModel newMatchModel = MatchModel(fields: Fields());
newMatchModel.fields.maleFirstName = ['33'];

相关问题