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

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


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.


When judging the array is empty, what do you use to judge it? Dart provides isNotEmpty, but it often reports errors in use, because your array at the time may not be [] and null. An extra layer of judgment is added when using all text

If your requirement was simply empty or null, you can use Dart's safe navigation operator to make it a bit terser:

as an isEmpty function that returns true if the argument is null or the empty string.

if (routeinfo["my_value"]?.isEmpty ?? true) {  // }

Another nice way to write this:

if ((value ?? '') == '') { ... }

Also do it like this

 return (list == null || list .isEmpty) ? Container() : Container(child:Text('The current array is not empty'));