flinq
Extended capabilities for collections. It's a bunch of shortcuts to avoid unnecesary boilerplate work with collections.
Getting Started
This package will help you to reduce the amount of boilerplate code by adding folowing extensions for Iterables:
-
getter
firstOrNulland methodfirstOrNullWherefor getting first value, and if it will not be found returnsnull -
getter
lastOrNulland methodlastOrNullWherefor getting last value, and if it will not be found returnsnull -
getter
singleOrNulland methodsingleOrNullWherefor getting single value, and if it will not be found returnsnull, and if there will be too many elements it'll also returnnull -
method
mapList, which maps collection and casts it toList -
method
whereList, which filters collection and casts it toList -
method
mapWhereList, which maps collection, then filters it and then casts it toList -
method
whereMapList, which filters collection, then maps collection and then casts it toList -
getter
notNullfor getting only not null values from the collection -
method
groupandgroupMapfor grouping a collection -
getter
min/minOrNullandmax/maxOrNullfor getting minimal or maximal value from collection ofComparables -
method
minWhere/minOrNullWhereandmaxWhere/maxOrNullWherefor getting minimal or maximal value from filtered collection ofComparables -
getter
sumandaveragefor getting sum and average from collection ofnums -
method
sumWhereandaverageWherefor getting sum and average from filtered collection ofnums -
getter
distinctand methoddistinctWherewhich will returnListwith unique values in collection -
method
unionandunionWherewhich will returnListwith union of two collections with only unique values in resulting collection -
method
intersectionandintersectionWherewhich will returnListwith elements that contains both collections with only unique values in resulting collection -
method
differenceanddifferenceWherewhich will returnListwith difference between two collections with only unique values in resulting collection
Examples
Iterable extension can be used like this:
Common
- firstOrNull and firstOrNullWhere
final firstOrNull = [].firstOrNull; // null
// or
final firstOrNull = [3, 6, 2, 7, 9].firstOrNullWhere((item) => item > 10); // null
- lastOrNull and lastOrNullWhere
final lastOrNull = [].lastOrNull; // null
// or
final lastOrNull = [3, 6, 2, 7, 9].lastOrNullWhere((item) => item > 10); // null
- singleOrNull and singleOrNullWhere
final singleOrNull = [].singleOrNull; // null
// or
final singleOrNull = [3, 6, 2, 7, 9].singleOrNullWhere((item) => item > 3); // null
- mapList
List<double> mappedList = [3, 6, 2, 7, 9].mapList((item) => item.toDouble());
- whereList
List<int> filteredList = [3, 6, 2, 7, 9].whereList((item) => item > 4);
- whereMapList
List<double> filteredMappedList = [3, 6, 2, 7, 9].whereMapList((item) => item > 4, (item) => item.toDouble());
- mapWhereList
List<double> mappedFilteredList = [3, 6, 2, 7, 9].mapWhereList((item) => item.toDouble(), (item) => item > 4);
- notNull
Iterable<int> notNullIterable = [3, 6, 2, null, 7, 9].notNull;
- group
final collection = [
Pet("rat", "Mike"),
Pet("dog", "Rex"),
Pet("cat", "Lucy"),
];
/*
<bool, List<Pet>>{
true: [
Pet("rat", "Mike"),
Pet("cat", "Lucy"),
],
false: [
Pet("dog", "Rex"),
],
}
*/
final group = collection.group((item) => item.name.endsWith('at'));
- groupMap
final collection = [
Pet("rat", "Mike"),
Pet("dog", "Rex"),
Pet("cat", "Lucy"),
];
/*
<bool, int>{
true: 2,
false: 1,
}
*/
final groupMapped = collection.groupMap(
(item) => item.name.endsWith('at'), (group) => group.length);
Comparable
- min and minOrNull
final min = [3, 6, 2, 7, 9].min; // 2
// or
final min = [].minOrNull; // null
- minWhere and minOrNullWhere
final min = [3, 6, 2, 7, 9].minWhere((_) => _ > 4); // 6
// or
final min = [3, 2].minOrNullWhere((_) => _ > 4); // null
- max and maxOrNull
final max = [3, 6, 2, 7, 9].max; // 9
// or
final max = [].maxOrNull; // null
- maxWhere and maxOrNullWhere
final max = [3, 6, 2, 7, 9].maxWhere((_) => _ < 4); // 3
// or
final max = [3, 2].maxOrNullWhere((_) => _ > 4); // null
Math
- sum
final sum = [3, 6, 2, 7, 9].sum; // 27
- average
final average = [1, 3, 5, 7, 4, 4].average; // 4
- sumWhere
final sum = [3, 6, 2, 7, 9].sumWhere((_) => _ > 4); // 22
- averageWhere
final average = [1, 3, 5, 7, 4, 4].averageWhere((_) => _ > 4); // 6
Set
- distinct
final collectionOne = [2, 5, 8, 2];
final distinctCollection = collectionOne.distinct; // [2, 5, 8]
- distinctWhere
final collectionOne = [2, 5, 8, 2];
final distinctCollection = collectionOne.distinctWhere((_) => _ > 4); // [5, 8]
- union
final collectionOne = [2, 5, 8, 2];
final collectionTwo = [1, 3, 5, 7];
final unitedCollection = collectionOne.union(collectionTwo); // [2, 5, 8, 1, 3, 7]
- unionWhere
final collectionOne = [2, 5, 8, 2];
final collectionTwo = [1, 3, 5, 7];
final unitedCollection = collectionOne.unionWhere(collectionTwo, (_) => _ > 4); // [5, 8, 7]
- intersection
final collectionOne = [2, 5, 8, 2];
final collectionTwo = [1, 3, 5, 7];
final intersectedCollection = collectionOne.intersection(collectionTwo); // [5]
- intersectionWhere
final collectionOne = [2, 5, 8, 2];
final collectionTwo = [1, 3, 5, 7];
final intersectedCollection = collectionOne.intersectionWhere(collectionTwo, (_) => _ < 4); // []
- difference
final collectionOne = [2, 5, 8, 2];
final collectionTwo = [1, 3, 5, 7];
final differedCollection = collectionOne.difference(collectionTwo); // [2, 8]
// or
final differedCollection = collectionTwo.difference(collectionOne); // [1, 3, 7]
- differenceWhere
final collectionOne = [2, 5, 8, 2];
final collectionTwo = [1, 3, 5, 7];
final differedCollection = collectionOne.differenceWhere(collectionTwo, (_) => _ < 4); // [2]
// or
final differedCollection = collectionTwo.differenceWhere(collectionOne, (_) => _ < 4); // [1, 3]
Feature requests and Bug reports
Feel free to post a feature requests or report a bug here.