必须初始化不可为 null 的实例字段 'buttonStartingYpoint'

Non-nullable instance field 'buttonStartingYpoint' must be initialized

这是我的模型class。我正在尝试获取 http 响应并将其打印到我的应用程序。

在第 12 行 DataModel 显示了这个错误

“不可为 null 的实例字段 'buttonStartingYpoint' 必须初始化。 尝试添加一个初始化表达式,或在此构造函数中添加一个字段初始化器,或将其标记为 'late'."

不可为 null 的实例字段 'buttonHeight' 必须初始化。 尝试添加初始化表达式,或在此构造函数中添加字段初始化器,或将其标记为 'late'

  String buttonLetter;
  int buttonHeight;
  int buttonWidth;
  int buttonStartingXpoint;
  int buttonStartingYpoint;

  DataModel(
      {required this.buttonLetter,
      required this.buttonHeight,
      required this.buttonWidth,
      required this.buttonStartingXpoint,
      required this.buttonStartingYpoint});

  DataModel.fromJson(Map<String, dynamic> json) {
    buttonLetter = json['Button_letter'];
    buttonHeight = json['Button_height'];
    buttonWidth = json['Button_width'];
    buttonStartingXpoint = json['Button_Starting_xpoint'];
    buttonStartingYpoint = json['Button_Starting_ypoint'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['Button_letter'] = this.buttonLetter;
    data['Button_height'] = this.buttonHeight;
    data['Button_width'] = this.buttonWidth;
    data['Button_Starting_xpoint'] = this.buttonStartingXpoint;
    data['Button_Starting_ypoint'] = this.buttonStartingYpoint;
    return data;
  }
}

我尝试添加 late 但后来它在 late 和 model 中都显示错误。我是 flutter 的新手

您必须在初始化列表中初始化它们。当您到达构造函数主体时,为时已晚。这是修复:

  DataModel.fromJson(Map<String, dynamic> json) :
    buttonLetter: json['Button_letter'],
    buttonHeight: json['Button_height'],
    buttonWidth: json['Button_width'],
    buttonStartingXpoint: json['Button_Starting_xpoint'],
    buttonStartingYpoint: json['Button_Starting_ypoint'];

供参考:https://dart.dev/guides/language/language-tour#constructors