//筛选第一个element
_firstWhereTest() {
var foundItem1 = items.firstWhere((element) => element.length > 5);
print(foundItem1);
var foundItem4 = items.firstWhere((element) => element.length > 10,
orElse: () => 'None!');
print(foundItem4);
final singleItems = ["acsddm"];
var foundItem2 = singleItems.singleWhere((element) => element.contains("e"),
orElse: () => "None!!");
print(foundItem2);
}
//any 有个符合条件返回true, every,全部符合条件返回true
_anyEveryTest() {
print(items.any((element) => element.length > 10));
print(items.every((element) => element.length > 5));
}
//where,赛选符合条件的element
_whereTest() {
const numbers = [4, 5, 1, -2, 3, 42];
var evenNumbers = numbers.where((number) => number.isEven);
for (final number in evenNumbers) {
print('$number is even.');
}
var numbersUntilNegative =
numbers.takeWhile((number) => !number.isNegative).toList();
print(numbersUntilNegative);
var numbersStartNegatives =
numbers.skipWhile((number) => !number.isNegative).toList();
print(numbersStartNegatives);
}
//重新赋值
_mapTest() {
var numbersByTwo =
const [1, -2, 3, 42].map((number) => number * 2).toList();
print('Numbers: $numbersByTwo');
}
网友评论