我是廣告,點擊一下吧!
標籤
#Flutter (17) 、 #PHP (9) 、 #Laravel (7) 、 #MySQL (5) 、 #Mac (5) 、 #Dart (5) 、 #Android (3) 、 #List (2) 、 #Widget (2) 、 #Carbon (2) 、 #IDE (2) 、 #VS Code (2) 、 #Linux (2) 、 #Shell Script (2) 、 #Cursor Pagination (1) 、 #Pagination (1) 、 #floorMonth (1) 、 #subMonthNoOverflow (1) 、 #資安 (1) 、 #addMonthNoOverflow (1) 、 #subMonth (1) 、 #MySQL 效能 (1) 、 #Mac M1 (1) 、 #個人空間 (1) 、 #Android Splash Screen (1) 、 #Android vector (1) 、 #SVG (1) 、 #Directory Structure (1) 、 #SQL Injection (1) 、 #createFromTimestamp (1)其他語言通常都是有叫做 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