Dart List Filter 的方法


#Flutter#Dart#List

其他語言通常都是有叫做 filter() 之類的 function,但在 Dart 想過濾 List 中的某些值時是使用 where(),滿特別的命名。
where() 回傳的是 Iterable 型別,所以必須再呼叫 toList() 轉回 List 型別。

void main() {
  List<int> list = [87, 8787];

  print(list.where((e) => e > 100).toList()); // [8787]
  print(list.where((e) => e < 100).toList()); // [87]
}

試一試吧:https://dartpad.dev/?id=8281264511bc4db31bbdc5c4379c042d


參考:
https://stackoverflow.com/questions/49578529
https://api.dart.dev/be/180791/dart-core/Iterable/where.html