我正在调用Web API并接收Profile模型作为响应。当我使用下面的代码时,它抛出了一个错误:
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);
}
Run Code Online (Sandbox Code Playgroud)
那是因为profile.message回报null。你可能想要
if(profile?.message?.isEmpty ?? true)
Run Code Online (Sandbox Code Playgroud)
?防止一个错误,如果在表达式结果的前部null和?? true在结果中true,如果表达的前一部分是null,并且因此治疗== null和isEmpty同为if(...)检查。
| 归档时间: |
|
| 查看次数: |
2253 次 |
| 最近记录: |