代码之家  ›  专栏  ›  技术社区  ›  El Hombre Sin Nombre

颤振-检查数组元素是否为空

  •  0
  • El Hombre Sin Nombre  · 技术社区  · 7 年前

    我正在检查对象是否有空元素。为此,我迭代一个列表并检查是否有数据,最后我需要检查这个列表的一个元素是否为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 回复  |  直到 7 年前
        1
  •  0
  •   Günter Zöchbauer    7 年前

    我会用

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

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