Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void main() {
- print('--- 1. forEach() ---');
- // 1. forEach() | Runs a function on each element in the list
- var fruits = ['banana', 'pineapple', 'watermelon'];
- fruits.forEach((fruit) => print(fruit)); // => banana pineapple watermelon
- print('--- 2. map() ---');
- // 2. map() | Produces a new list after transforming each element in a given list
- var mappedFruits = fruits.map((fruit) => 'I love $fruit').toList();
- print(mappedFruits); // => ['I love banana', 'I love pineapple', 'I love watermelon']
- print('--- 3. contains() ---');
- // 3. contains() | Checks to confirm that the given element is in the list
- var numbers = [1, 3, 2, 5, 4];
- print(numbers.contains(2)); // => true
- print('--- 4. sort() ---');
- // 4. sort() | Order the elements based on the provided ordering function
- numbers.sort((num1, num2) => num1 - num2);
- print(numbers); // => [1, 2, 3, 4, 5]
- print('--- 5. reduce(), fold() ---');
- // 5. reduce(), fold() | Compresses the elements to a single value, using the given function
- var sum = numbers.reduce((current, next) => current + next);
- print(sum); // => 15
- const initialValue = 10;
- var sum2 = numbers.fold(initialValue, (current, next) => current + next);
- print(sum2); // => 25
- print('--- 6. every() ---');
- // 6. every() | Confirms that every element satisfies the test
- List<Map<String, dynamic>> users = [
- { "name": 'Jane', "age": 18 },
- { "name": 'Jim', "age": 21 },
- { "name": 'Mary', "age": 23 },
- ];
- var is18AndOver = users.every((user) => user["age"] >= 18);
- print(is18AndOver); // => true
- var hasNamesWithJ = users.every((user) => user["name"].startsWith('J'));
- print(hasNamesWithJ); // => false
- print('--- 7. where(), firstWhere(), singleWhere()');
- // 7. where(), firstWhere(), singleWhere() | Returns a collection of elements that satisfy a test.
- // firstWhere() returns the first match in the list, while singleWhere() returns the first match provided there is exactly one match.
- var over21s = users.where((user) => user["age"] > 20);
- print(over21s.length); // => 1
- var under18s = users.singleWhere((user) => user["age"] < 18, orElse: () => null);
- print(under18s); // => null
- var nameJ = users.firstWhere((user) => user["name"].startsWith('J'), orElse: () => null);
- print(nameJ); // => {name: Jane, age: 18}
- print('--- 8. take(), skip() ---');
- // take(), skip() | Returns a collection while including or skipping elements
- var fiboNumbers = [1, 2, 3, 5, 8, 13, 21];
- print(fiboNumbers.take(3).toList()); // => [1, 2, 3]
- print(fiboNumbers.skip(5).toList()); // => [13, 21]
- print(fiboNumbers.take(3).skip(2).take(1).toList()); // => [3]
- print('--- 9. List.from() ---');
- // List.from() | Creates a new list from the given collection
- var clonedFiboNumbers = List.from(fiboNumbers);
- print(clonedFiboNumbers); // => [1, 2, 3, 5, 8, 13, 21]
- print('--- 10. expand() ---');
- // expand() | Expands each element into zero or more elements
- var pairs = [[1, 2], [3, 4]];
- var flattened = pairs.expand((pair) => pair).toList();
- print('Flattened result: $flattened'); // => [1, 2, 3, 4]
- var input = [1, 2, 3];
- var duplicated = input.expand((i) => [i, i]).toList();
- print(duplicated); // => [1, 1, 2, 2, 3, 3, 4, 4]
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement