flutter - 颤振:NoSuchMethodError:在 null 上调用了 getter 'isEmpty'
问题描述
我正在调用 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);
}
解决方案
那是因为profile.message
返回null
. 你可能想要
if(profile?.message?.isEmpty ?? true)
?
如果表达式的前一部分导致错误,则防止错误,如果表达式的前一部分是,则导致错误,null
因此对检查进行相同处理。?? true
true
null
== null
isEmpty
if(...)
推荐阅读
- java - Gradle 要求将某些版本的库放入 17.0.3
- java - 使用 Angularjs 向服务器发送和接收数据
- azure - Azure Application Insights - 获取应用列表及其 ID
- html - Thymeleaf:如何隐藏子菜单
- elixir - 如何在 `iex` 中列出我的所有别名?
- python - JSON文件不会加载和打印
- android - 为什么有来电时android会取消对silenseRinger()的支持?
- javascript - 我如何用这个 JavaScript 方法来比较参数和对象转换值?
- python - 对列表中的每个项目重复整个 df
- python - 如何使用 flatten_json 递归地展平嵌套的 JSON