简体   繁体   中英

_TypeError (type 'Null' is not a subtype of type 'String') Flutter-jsonserialisable

I used the json_serializable package to convert data to JSON format as a Dart class and I get an error, here is the deserialized class code

import 'package:json_annotation/json_annotation.dart';

part 'country.g.dart';

@JsonSerializable()
class Country {
  final String name;
  final List<String> topLevelDomain;
  final String alpha2Code;
  final String alpha3Code;
  final List<String> callingCodes;
  final String capital;
  final List<String> altSpellings;
  final String region;
  final String continent;
  final int population;
  final List<int> latlng;
  final String demonym;
  final int area;
  final double gini;
  final List<String> timezones;
  final List<String> borders;
  final String nativeName;
  final String numericCode;
  final List<Currencies> currencies;
  final List<Languages> languages;
  final Translations translations;
  final List<String> flags;
  final List<RegionalBlocs> regionalBlocs;
  final String cioc;
  final bool independent;

  Country(
      {required this.name,
      required this.topLevelDomain,
      required this.alpha2Code,
      required this.alpha3Code,
      required this.callingCodes,
      required this.capital,
      required this.altSpellings,
      required this.region,
      required this.continent,
      required this.population,
      required this.latlng,
      required this.demonym,
      required this.area,
      required this.gini,
      required this.timezones,
      required this.borders,
      required this.nativeName,
      required this.numericCode,
      required this.currencies,
      required this.languages,
      required this.translations,
      required this.flags,
      required this.regionalBlocs,
      required this.cioc,
      required this.independent});

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

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

@JsonSerializable()
class Currencies {
  final String code;
  final String name;
  final String symbol;

  Currencies({
    required this.code,
    required this.name,
    required this.symbol,
  });

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

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

@JsonSerializable()
class Languages {
  final String iso6391;
  final String iso6392;
  final String name;
  final String nativeName;

  Languages({
    required this.iso6391,
    required this.iso6392,
    required this.name,
    required this.nativeName,
  });

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

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

@JsonSerializable()
class Translations {
  final String br;
  final String pt;
  final String nl;
  final String hr;
  final String fa;
  final String de;
  final String es;
  final String fr;
  final String ja;
  final String it;
  final String hu;

  Translations(
      {required this.br,
      required this.pt,
      required this.nl,
      required this.hr,
      required this.fa,
      required this.de,
      required this.es,
      required this.fr,
      required this.ja,
      required this.it,
      required this.hu});

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

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

@JsonSerializable()
class RegionalBlocs {
  final String acronym;
  final String name;
  final List<String> otherNames;
  final List<String> otherAcronyms;

  RegionalBlocs({
    required this.acronym,
    required this.name,
    required this.otherNames,
    required this.otherAcronyms,
  });

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

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

and here is the file generated by the package.

// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'country.dart';

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

Country _$CountryFromJson(Map<String, dynamic> json) => Country(
      name: json['name'] as String,
      topLevelDomain: (json['topLevelDomain'] as List<dynamic>)
          .map((e) => e as String)
          .toList(),
      alpha2Code: json['alpha2Code'] as String,
      alpha3Code: json['alpha3Code'] as String,
      callingCodes: (json['callingCodes'] as List<dynamic>)
          .map((e) => e as String)
          .toList(),
      capital: json['capital'] as String,
      altSpellings: (json['altSpellings'] as List<dynamic>)
          .map((e) => e as String)
          .toList(),
      region: json['region'] as String,

this is the line that throws an exception

      continent: json['continent'] as String,
//...

      population: json['population'] as int,
      latlng: (json['latlng'] as List<dynamic>).map((e) => e as int).toList(),
      demonym: json['demonym'] as String,
      area: json['area'] as int,
      gini: (json['gini'] as num).toDouble(),
      timezones:
          (json['timezones'] as List<dynamic>).map((e) => e as String).toList(),
      borders:
          (json['borders'] as List<dynamic>).map((e) => e as String).toList(),
      nativeName: json['nativeName'] as String,
      numericCode: json['numericCode'] as String,
      currencies: (json['currencies'] as List<dynamic>)
          .map((e) => Currencies.fromJson(e as Map<String, dynamic>))
          .toList(),
      languages: (json['languages'] as List<dynamic>)
          .map((e) => Languages.fromJson(e as Map<String, dynamic>))
          .toList(),
      translations:
          Translations.fromJson(json['translations'] as Map<String, dynamic>),
      flags: (json['flags'] as List<dynamic>).map((e) => e as String).toList(),
      regionalBlocs: (json['regionalBlocs'] as List<dynamic>)
          .map((e) => RegionalBlocs.fromJson(e as Map<String, dynamic>))
          .toList(),
      cioc: json['cioc'] as String,
      independent: json['independent'] as bool,
    );

Map<String, dynamic> _$CountryToJson(Country instance) => <String, dynamic>{
      'name': instance.name,
      'topLevelDomain': instance.topLevelDomain,
      'alpha2Code': instance.alpha2Code,
      'alpha3Code': instance.alpha3Code,
      'callingCodes': instance.callingCodes,
      'capital': instance.capital,
      'altSpellings': instance.altSpellings,
      'region': instance.region,
      'continent': instance.continent,
      'population': instance.population,
      'latlng': instance.latlng,
      'demonym': instance.demonym,
      'area': instance.area,
      'gini': instance.gini,
      'timezones': instance.timezones,
      'borders': instance.borders,
      'nativeName': instance.nativeName,
      'numericCode': instance.numericCode,
      'currencies': instance.currencies,
      'languages': instance.languages,
      'translations': instance.translations,
      'flags': instance.flags,
      'regionalBlocs': instance.regionalBlocs,
      'cioc': instance.cioc,
      'independent': instance.independent,
    };

Currencies _$CurrenciesFromJson(Map<String, dynamic> json) => Currencies(
      code: json['code'] as String,
      name: json['name'] as String,
      symbol: json['symbol'] as String,
    );

Map<String, dynamic> _$CurrenciesToJson(Currencies instance) =>
    <String, dynamic>{
      'code': instance.code,
      'name': instance.name,
      'symbol': instance.symbol,
    };

Languages _$LanguagesFromJson(Map<String, dynamic> json) => Languages(
      iso6391: json['iso6391'] as String,
      iso6392: json['iso6392'] as String,
      name: json['name'] as String,
      nativeName: json['nativeName'] as String,
    );

Map<String, dynamic> _$LanguagesToJson(Languages instance) => <String, dynamic>{
      'iso6391': instance.iso6391,
      'iso6392': instance.iso6392,
      'name': instance.name,
      'nativeName': instance.nativeName,
    };

Translations _$TranslationsFromJson(Map<String, dynamic> json) => Translations(
      br: json['br'] as String,
      pt: json['pt'] as String,
      nl: json['nl'] as String,
      hr: json['hr'] as String,
      fa: json['fa'] as String,
      de: json['de'] as String,
      es: json['es'] as String,
      fr: json['fr'] as String,
      ja: json['ja'] as String,
      it: json['it'] as String,
      hu: json['hu'] as String,
    );

Map<String, dynamic> _$TranslationsToJson(Translations instance) =>
    <String, dynamic>{
      'br': instance.br,
      'pt': instance.pt,
      'nl': instance.nl,
      'hr': instance.hr,
      'fa': instance.fa,
      'de': instance.de,
      'es': instance.es,
      'fr': instance.fr,
      'ja': instance.ja,
      'it': instance.it,
      'hu': instance.hu,
    };

RegionalBlocs _$RegionalBlocsFromJson(Map<String, dynamic> json) =>
    RegionalBlocs(
      acronym: json['acronym'] as String,
      name: json['name'] as String,
      otherNames: (json['otherNames'] as List<dynamic>)
          .map((e) => e as String)
          .toList(),
      otherAcronyms: (json['otherAcronyms'] as List<dynamic>)
          .map((e) => e as String)
          .toList(),
    );

Map<String, dynamic> _$RegionalBlocsToJson(RegionalBlocs instance) =>
    <String, dynamic>{
      'acronym': instance.acronym,
      'name': instance.name,
      'otherNames': instance.otherNames,
      'otherAcronyms': instance.otherAcronyms,
    };

please help me, it's the end of two months that I started Flutter and Dart and this problem blocks me completely, thank you in advance.

Change continent: json['continent'] as String to

continent: json['continent']??""

If you wish to convert it from the generator change the value of continent to null and generate. Which should result in it's type being dynamic

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM