rroviedo
rroviedo

Reputation: 3

i cant figure out why this error type 'Null' is not a subtype of type 'int' in type cast

Flutter 3.3.9 • channel stable • https://github.com/flutter/flutter.git Framework • revision b8f7f1f986 (hace 2 semanas) • 2022-11-23 06:43:51 +0900 Engine • revision 8f2221fbef Tools • Dart 2.18.5 • DevTools 2.15.0

hello, best regards and I hope you are well, I have spent the last few hours trying to solve the error in the title of the issue, and I have not been able to, could you explain to me a way to solve it, I have attached the model that I am using and the response of the request to an api, thank you very much in advance

base_response_model.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);
}

get_municipalities_by_province_response_model.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);
}

municipality_model.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_response.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
    }
  ]
}

Generated code

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'base_response_model.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

BaseResponseModel _$BaseResponseModelFromJson(Map<String, dynamic> json) =>
    BaseResponseModel(
      page: json['page'] as int,
      pageSize: json['page_size'] as int,
      total: json['total'] as int,
      pages: json['pages'] as int,
      prevPage: json['prev_page'] ?? 0,
      nextPage: json['next_page'] ?? 0,
    );

Map<String, dynamic> _$BaseResponseModelToJson(BaseResponseModel instance) =>
    <String, dynamic>{
      'page': instance.page,
      'page_size': instance.pageSize,
      'total': instance.total,
      'pages': instance.pages,
      'prev_page': instance.prevPage,
      'next_page': instance.nextPage,
    };

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'get_municipalities_by_province_response_model.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

GetMunicipalitiesByProvinceResponseModel
    _$GetMunicipalitiesByProvinceResponseModelFromJson(
            Map<String, dynamic> json) =>
        GetMunicipalitiesByProvinceResponseModel(
          page: json['page'] as int,
          pageSize: json['page_size'] as int,
          total: json['total'] as int,
          pages: json['pages'] as int,
          prevPage: json['prev_page'] ?? 0,
          nextPage: json['next_page'] ?? 0,
          data: (json['data'] as List<dynamic>)
              .map((e) => MunicipalityModel.fromJson(e as Map<String, dynamic>))
              .toList(),
        );

Map<String, dynamic> _$GetMunicipalitiesByProvinceResponseModelToJson(
        GetMunicipalitiesByProvinceResponseModel instance) =>
    <String, dynamic>{
      'page': instance.page,
      'page_size': instance.pageSize,
      'total': instance.total,
      'pages': instance.pages,
      'prev_page': instance.prevPage,
      'next_page': instance.nextPage,
      'data': instance.data,
    };

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'municipality_model.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

MunicipalityModel _$MunicipalityModelFromJson(Map<String, dynamic> json) =>
    MunicipalityModel(
      id: json['id'] as int,
      name: json['name'] as String,
      parent: json['parent'] as int,
    );

Map<String, dynamic> _$MunicipalityModelToJson(MunicipalityModel instance) =>
    <String, dynamic>{
      'id': instance.id,
      'name': instance.name,
      'parent': instance.parent,
    };

I am using flutter, null safety

Upvotes: 0

Views: 649

Answers (1)

Kevin Moore
Kevin Moore

Reputation: 6171

It looks like a field is missing (or null) in the JSON causing that problem. The line number should point you to the right spot!

Upvotes: 0

Related Questions