Flutter-检查数组元素是否为空

时间:2018-12-05 19:20:08

标签: dart flutter

我正在尝试检查object是否具有null元素。为此,我迭代了一个列表并检查是否有数据,最后我需要检查此列表中的一个元素是否为空。如果为null,则不添加到数据列表;如果有数据,则添加到数据列表。

List data = List();

final responseJson = json.decode(response.body) as List;
     if (responseJson!=null){
          for (Map data in responseJson) {
               if(data[‘name']=!null){ <- I need to ignore elements that doesn't have name, but all element (including elements without name) pass for this if.
                    list.add(response[‘name’]):
               }
          }
     }

忽略不包含名称的元素并在列表中添加具有名称的元素的正确方法是什么?

1 个答案:

答案 0 :(得分:0)

我会用

for (Map data in responseJson.where((d) => d.containsKey('name'))) {

可能需要

for (Map data in responseJson.where((d) => (d as Map).containsKey('name'))) {