Null safety and checking for `Null`

99 views Asked by At

this piece of code

  static fromJson(Map<String, dynamic> j) {
    return _TheCountry(
      name: j["Name"]??"",
      dialCode: j["DialCode"]??"",
      iso2Code: j["Iso2"]??"",
      englishName: j["EnglishName"]??"",
      iso3Code: j["Iso3"]??"",
      currency: j["Currency"]??"",
      capital: j["Capital"]??"",
      dialLengths: List.castFrom(getDialLengths(j["DialLength"]??"",)),
    );
  }

worked previously, but now the test fails by throwing,

Failed to load "xxx/xxxx/the_country_number_test.dart": type 'Null' is not a subtype of type 'String'

the error is thrown while accessing j['DialCode'] ('DialCode' property might not exist), AFAIK accessing DialCode property by j['DialCode'] is giving me Null object but how do i get away with using j['DialCode']??""

1

There are 1 answers

0
AudioBubble On

try to use contains like this

j.contains("DialCode") ? j["DialCode"] : “”