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

时间:2018-12-14 10:23:24

标签: 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);
}

2 个答案:

答案 0 :(得分:2)

这是因为public class MyBootCompletedReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { Intent intentService = new Intent(context, TestService.class); ContextCompat.startForegroundService(context, intentService); } } } 返回profile.message。您可能想要

null
如果表达式的前一部分为if(profile?.message?.isEmpty ?? true)

?可防止错误,如果表达式的前一部分为{{1},则null的结果为?? true },因此true检查中将null== null视为相同。

答案 1 :(得分:0)

判断数组为空时,用什么来判断? Dart提供了isNotEmpty,但是在使用中经常报错,因为你当时的数组可能不是[]和null。使用全文本时多加一层判断

如果您的要求只是空的或空的,您可以使用 Dart 的安全导航操作符使其更简洁:

作为 isEmpty 函数,如果参数为 null 或空字符串则返回 true

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

另一种写法:

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

也这样做

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