If you miss an extension, please open an issue or pull request
On this page you can find some of the extensions. Take a look at the docs to see all of them.
Add the following to your pubspec.yaml
:
dependencies:
dartx: any
After you import the library, you can use the extensions.
import 'package:dartx/dartx.dart';
final slice = [1, 2, 3, 4, 5].slice(1, -2); // [2, 3, 4]
Returns elements at indices between start
(inclusive) and end
(inclusive).
final list = [0, 1, 2, 3, 4, 5];
final last = list.slice(-1); // [5]
final lastHalf = list.slice(3); // [3, 4, 5]
final allButFirstAndLast = list.slice(1, -2); // [1, 2, 3, 4]
Sort lists by multiple properties.
final dogs = [
Dog(name: "Tom", age: 3),
Dog(name: "Charlie", age: 7),
Dog(name: "Bark", age: 1),
Dog(name: "Cookie", age: 4),
Dog(name: "Charlie", age: 2),
];
final sorted = dogs
.sortedBy((dog) => dog.name)
.thenByDescending((dog) => dog.age);
// Bark, Cookie, Charlie (7), Charlie (2), Tom
Get distinct elements from a list.
final list = ['this', 'is', 'a', 'test'];
final distinctByLength = list.distinctBy((it) => it.length); // ['this', 'is', 'a']
Get a new lazy Iterable
of all elements from all collections in a collection.
final nestedList = [[1, 2, 3], [4, 5, 6]];
final flattened = nestedList.flatten(); // [1, 2, 3, 4, 5, 6]
Chunk entries as long as long as two elements match a predicate:
final list = [1, 2, 4, 9, 10, 11, 12, 15, 16, 19, 20, 21];
final increasingSubSequences = list.chunkWhile((a, b) => a + 1 == b);
// increasingSubSequences = [[1, 2], [4], [9], [10, 11, 12], [15, 16], [19, 20, 21]]
splitWhen
is the opposite of chunkWhile
that starts a new chunk every time
the predicate didn't match.
Returns a copy of the string having its first letter uppercased, or the original string, if it's empty or already starts with an upper case letter.
final word = 'abcd'.capitalize(); // Abcd
final anotherWord = 'Abcd'.capitalize(); // Abcd
Use .characters
from the official characters package.
Get a list of single character strings from a string. Supports emojis.
final chars = 'family👨👨👧👦'.chars; // ['f', 'a', 'm', 'i', 'l', 'y', '👨👨👧👦']