颤振:NoSuchMethodError:在null上调用吸气剂"isEmpty" [英] flutter: NoSuchMethodError: The getter 'isEmpty' was called on null

查看:76
本文介绍了颤振:NoSuchMethodError:在null上调用吸气剂"isEmpty"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在调用Web API并接收Profile模型作为响应.当我使用下面的代码时,它抛出了一个错误:

I am calling web API and receiving the Profile model as a response. When I am using the below code then it is throwing me an error:

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 .您可能想要

That is because profile.message returns null. You might want

if(profile?.message?.isEmpty ?? true)

?可以防止错误,如果表达式的前一部分导致 null ?如果表达式的前一部分为 null ,则true 生成 true ,因此将 == null isEmpty if(...)检查相同.

? prevents an error if the previous part of the expression results in null and ?? true results in true if the previous part of the expression is null and therefore treats == null and isEmpty the same for the if(...) check.

这篇关于颤振:NoSuchMethodError:在null上调用吸气剂"isEmpty"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆