我正在调用 Web API 并接收配置文件模型作为响应。当我使用下面的代码时,它会抛出一个错误:
try{
if(profile.message.isEmpty){
Navigator.of(context).pushNamed("/home");
}else if(profile == null){
_showDialog("Please check your internet connection");
}else{
_showDialog("Please enter valid credentials");
}
}catch(exception){
print(exception);
}
那是因为profile.message
回报null
。你可能想要
if(profile?.message?.isEmpty ?? true)
?
如果表达式的前一部分结果为,则可以防止错误null
and ?? true
结果是true
如果表达式的前一部分是null
因此对待== null
and isEmpty
同样对于if(...)
check.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)