Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter: NoSuchMethodError: The getter 'isEmpty' was called on null

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);
}
like image 687
Code Hunter Avatar asked Dec 14 '18 10:12

Code Hunter


1 Answers

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

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

? 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.

like image 165
Günter Zöchbauer Avatar answered Nov 20 '22 13:11

Günter Zöchbauer