Removes falsy values from a list.

Use Iterable.removeWhere() in combination with the cascade operator (..) to filter out falsy values (false, null).

代码实现

List<T> compact<T>(List<T> lst) {
  return lst..removeWhere((v) => [null, false].contains(v));
}

使用样例

compact([0, 1, false, 2, 3, 'a', null, 'b']); // [0, 1, 2, 3, 'a', 'b']