1

我有 getter 获取我的FilterModelRow. 如果条件为空,我想跳过将数据插入列表,最好内联

 get getFilterRows {
    return [FilterRowModel(Icon(Icons.title), 'Age', 'equal', age),
            FilterRowModel(Icon(Icons.title), 'Age', 'min', minAge),
            FilterRowModel(Icon(Icons.title), 'Age', 'max', maxAge)
           ];

  }

我试过了

...
age != null FilterRowModel(Icon(Icons.title), 'Age', 'equal', age): null
...

但是那个以错误结尾的插入null。那么如果满足条件,如何完全跳过将行添加到列表中

简化版

  var age = null;

  List<int> myList = [age!=null ? age : null];

  print(myList); //--> return [null] and I want to return empty list []
4

1 回答 1

1

如果你告诉你的列表插入一个空值,它会。

现在你有两个选择:

1 - 您可以实例化您的列表并添加不为空的值

List<int> myList = [];
if (age != null) myList.add(age);

2 - 您可以使用 removeWhere 方法从列表中删除空值

myList.removeWhere((value) => value == null);
于 2020-03-30T22:45:31.330 回答