美文网首页
可迭代集合

可迭代集合

作者: XH小子 | 来源:发表于2023-01-08 15:39 被阅读0次
 //筛选第一个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');
  }

相关文章

网友评论

      本文标题:可迭代集合

      本文链接:https://www.haomeiwen.com/subject/abuucdtx.html