collection/collection.dart

dartの基本クラスを便利に使えるようにするライブラリらしい

使用例

import 'package:collection/collection.dart';

void main() {
  // Listの拡張機能
  List<int> numbers = [1, 2, 3, 4, 5];
  print(numbers.containsAll([1, 2])); // true
  print(numbers.indexOfFirst((n) => n > 3)); // 3

  // Setの拡張機能
  Set<int> set1 = {1, 2, 3};
  Set<int> set2 = {3, 4, 5};
  print(set1.union(set2)); // {1, 2, 3, 4, 5}

  // Mapの拡張機能
  Map<String, int> ages = {'Alice': 30, 'Bob': 25};
  print(ages.containsKey('Alice')); // true
  print(ages.mapValues((key, value) => value + 1)); // {Alice: 31, Bob: 26}

  // Equality
  List<int> list1 = [1, 2, 3];
  List<int> list2 = [1, 2, 3];
  print(ListEquality().equals(list1, list2)); // true

  // Grouping
  List<String> words = ['apple', 'banana', 'apricot'];
  var grouped = groupBy(words, (String word) => word[0]);
  print(grouped); // {a: [apple, apricot], b: [banana]}
}
トップ   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS