flutter 我无法理解为什么此错误类型“Null”不是类型转换中类型“int”的子类型

iqxoj9l9  于 2022-12-14  发布在  Flutter
关注(0)|答案(1)|浏览(97)

Flutter3.3.9 ·通道稳定·https://github.com/flutter/flutter.git框架·修订版b8 f7 f1 f986(为期2周)· 2022-11-23 06:43:51 +0900引擎·修订版8 f2221 fbef工具· Dart 2.18.5 · DevTools 2.15.0
你好,最好的问候,我希望你是好的,我已经花了过去的几个小时试图解决问题的标题中的错误,我一直无法,你能解释给我一个方法来解决它,我已经附上了我正在使用的模型和请求的响应到一个API,非常感谢你提前

基本响应模型.dart

import 'package:json_annotation/json_annotation.dart';

part 'base_response_model.g.dart';

@JsonSerializable()
class BaseResponseModel {
  final int page;

  @JsonKey(name: 'page_size')
  final int pageSize;

  final int total;
  final int pages;

  @JsonKey(name: 'prev_page', defaultValue: 0)
  final dynamic prevPage;

  @JsonKey(name: 'next_page', defaultValue: 0)
  final dynamic nextPage;

  BaseResponseModel({
    required this.page,
    required this.pageSize,
    required this.total,
    required this.pages,
    required this.prevPage,
    required this.nextPage,
  });

  factory BaseResponseModel.fromJson(Map<String, dynamic> json) =>
      _$BaseResponseModelFromJson(json);

  Map<String, dynamic> toJson() => _$BaseResponseModelToJson(this);
}

获取省响应模型的直辖市.dart

import 'package:delivery/app/data/models/andariego/andariego_models/municipality_model.dart';
import 'package:delivery/app/data/models/andariego/andariego_response_models/base_response_model.dart';
import 'package:json_annotation/json_annotation.dart';

part 'get_municipalities_by_province_response_model.g.dart';

@JsonSerializable()
class GetMunicipalitiesByProvinceResponseModel extends BaseResponseModel {
  final List<MunicipalityModel> data;

  GetMunicipalitiesByProvinceResponseModel({
    required super.page,
    required super.pageSize,
    required super.total,
    required super.pages,
    required super.prevPage,
    required super.nextPage,
    required this.data,
  });

  factory GetMunicipalitiesByProvinceResponseModel.fromJson(
          Map<String, dynamic> json) =>
      _$GetMunicipalitiesByProvinceResponseModelFromJson(json);

  @override
  Map<String, dynamic> toJson() =>
      _$GetMunicipalitiesByProvinceResponseModelToJson(this);
}

自治市模型.dart

import 'package:delivery/app/data/models/andariego/andariego_models/base_andariego_model.dart';
import 'package:json_annotation/json_annotation.dart';

part 'municipality_model.g.dart';

@JsonSerializable()
class MunicipalityModel extends BaseAndariegoModel {
  final int parent;

  MunicipalityModel({
    required super.id,
    required super.name,
    required this.parent,
  });

  factory MunicipalityModel.fromJson(Map<String, dynamic> json) =>
      _$MunicipalityModelFromJson(json);

  @override
  Map<String, dynamic> toJson() => _$MunicipalityModelToJson(this);
}

API_响应.json

{
  "page": 1,
  "page_size": 20,
  "total": 11,
  "pages": 1,
  "prev_page": null,
  "next_page": null,
  "data": [
    {
      "id": 1188,
      "name": "Consolación del Sur",
      "parent": 58
    },
    {
      "id": 1132,
      "name": "Guane",
      "parent": 58
    },
    {
      "id": 1125,
      "name": "La Palma",
      "parent": 58
    },
    {
      "id": 1124,
      "name": "Los Palacios",
      "parent": 58
    },
    {
      "id": 1186,
      "name": "Mantua",
      "parent": 58
    },
    {
      "id": 1182,
      "name": "Minas de Matahambre",
      "parent": 58
    },
    {
      "id": 1189,
      "name": "Pinar del Rio",
      "parent": 58
    },
    {
      "id": 1165,
      "name": "Sandino",
      "parent": 58
    },
    {
      "id": 1133,
      "name": "San Juan y Martínez",
      "parent": 58
    },
    {
      "id": 1187,
      "name": "San Luis",
      "parent": 58
    },
    {
      "id": 1169,
      "name": "Viñales",
      "parent": 58
    }
  ]
}

生成的代码

第一个
我用的是Flutter,零安全

x6h2sr28

x6h2sr281#

看起来JSON中的某个字段丢失(或为空)导致了这个问题。行号应该指向正确的位置!

相关问题