颤振:NoSuchMethodError:在空调用getter'isEmpty'

Cod*_*ter 2 dart flutter

我正在调用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)

Gün*_*uer 5

那是因为profile.message回报null。你可能想要

if(profile?.message?.isEmpty ?? true)
Run Code Online (Sandbox Code Playgroud)

?防止一个错误,如果在表达式结果的前部null?? true在结果中true,如果表达的前一部分是null,并且因此治疗== nullisEmpty同为if(...)检查。