Sitemap
codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

Top 10 Array utility methods you should know (Dart) šŸŽÆ

--

Press enter or click to view image in full size
Background photo by XiaoXiao Sun on Unsplash

As part of my venture into client-side application development with Dart, I began exploring the way one could go about working with Lists or Array types. Aside from the documentation being comprehensive, I was also able to find support on the StackOverflow community and successfully achieved what was needed.

→ Continue reading on my blog

In today’s article we’ll be looking at the ā€œbatteries-includedā€ notion of Dart, in particular the inbuilt utilities for working with Lists. I’ve hand-picked 10 of the most common ones you show know for your next app. I’ve also prepared the example snippets so you could play with those yourself 😁

So, shall we begin?

1. forEach()

Runs a function on each element in the list

var fruits = [ā€˜banana’, ā€˜pineapple’, ā€˜watermelon’];
fruits.forEach((fruit) => print(fruit)); // => banana pineapple watermelon

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’]

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

4. sort()

Order the elements based on the provided ordering function

numbers.sort((num1, num2) => num1 - num2); // => [1, 2, 3, 4, 5]

5. reduce(), fold()

Compresses the elements to a single value, using the given function

var sum = numbers.reduce((curr, next) => curr + next);
print(sum); // => 15
const initialValue = 10;
var sum2 = numbers.fold(initialValue, (curr, next) => curr + next);
print(sum2); // => 25

6. every()

Confirms that every element satisfies the test

List<Map<String, dynamic>> users = [
{ ā€œnameā€: ā€˜John’, ā€œageā€: 18 },
{ ā€œnameā€: ā€˜Jane’, ā€œ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

7. where(), firstWhere(), singleWhere()

Returns a collection of elements that satisfy a test.

// See #6 for users list
var over21s = users.where((user) => user[ā€œageā€] > 21);
print(over21s.length); // => 1
var nameJ = users.firstWhere((user) => user[ā€œnameā€].startsWith(ā€˜J’), orElse: () => null);
print(nameJ); // => {name: John, age: 18}
var under18s = users.singleWhere((user) => user[ā€œageā€] < 18, orElse: () => null);
print(under18s); // => null

firstWhere() returns the first match in the list, while singleWhere() returns the first match provided there is exactly one match.

8. 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]

9. List.from()

Creates a new list from the given collection

var clonedFiboNumbers = List.from(fiboNumbers);
print(ā€˜Cloned list: $clonedFiboNumbers’);

As of Dart 2.0, the new keyword is optional when instantiating objects.

10. 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]

Conclusion

I hope this has been insightful and if this is your first exposure to Dart, read my first steps tutorial to grasp the basics. The example snippets for this article are available on DartPad.

I also run a YouTube channel teaching subscribers to develop full-stack applications with Dart. Become a subscriber to receive updates when new videos are released.

Like, share and follow mešŸ˜ for more content on Dart.

And this concludes the tutorial. Thanks for reading.

→ Read more Dart tutorials on my blog

What to check out next

  1. List<E> class Documentation
  2. Full-stack web development with Dart
  3. Dart screencasts on Egghead.io

āœ‰ļø Subscribe to CodeBurst’s once-weekly Email Blast, 🐦 Follow CodeBurst onTwitter, view šŸ—ŗļø The 2018 Web Developer Roadmap, and šŸ•øļø Learn Full Stack Web Development.

--

--

codeburst
codeburst

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Jermaine Oppong
Jermaine Oppong

Written by Jermaine Oppong

Christian | Web Developer | Egghead.io instructor | Sharing exclusive Dart content on my Blog → https://creativebracket.com | @creativ_bracket

Responses (10)