【发布时间】:2020-12-19 06:03:11
【问题描述】:
这段代码
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"]??"",)),
);
}
以前工作过,但现在测试因抛出而失败,
Failed to load "xxx/xxxx/the_country_number_test.dart": type 'Null' is not a subtype of type 'String'
访问j['DialCode'] 时抛出错误('DialCode' 属性可能不存在),
AFAIK 通过j['DialCode'] 访问 DialCode 属性给了我Null 对象,但我如何摆脱使用j['DialCode']??""
【问题讨论】:
-
请检查:将 DialCode 的类型从 'String' 更改为 'var'
-
@Uni 是的,这是我正在休闲的事情,
nonNullableString = nullableString ?? ""但仍然可以为 nullableString 这里是Null而不是null
标签: flutter dart dart-null-safety