有没有办法快速检查解码的JSON对象中的空条目?
例如:
final responseJson = json.decode(response.body);
这将返回以下内容:
responseJson['result']['mydata']['account'] = 'Joe Doe';
为了检查'mydata'部分是否为空,我必须执行以下操作:
if(responseJson != null)
{
if(responseJson['result'] != null)
{
if(responseJson['result']['mydata'] != null)
...
这真的很难看 . 怎么做这样:
if((responseJson != null) && (responseJson['result'] != null) && (responseJson['result']['mydata'] != null))
{
}
在Dart中,如果某些中间项为空(即['result']),则会给出异常 .
有空知识的运算符,如:
obj?.method()
但如何将它们与解码的JSON Map 对象一起使用?
2 回答
如果你想设置一个像in的值
这可能对你有用
阅读你可以使用
你应该做什么而不是直接使用JSON对象是将它转换为Dart类 . 然后使用null aware运算符 .
您可以使用
json_serializable
或built_value
生成fromJSON
类构造函数 .或者您可以手动编写它们: