Manipulate Lists/Arrays in Dart

Jermaine Oppong
InstructorJermaine Oppong
Share this video with your friends

Social Share Links

Send Tweet

We will learn how to work with Lists using a variety of methods made available in the dart:core library. We will explore the top methods for working with List type collections.

Learn more about Lists at https://api.dartlang.org/stable/2.2.0/dart-core/List-class.html

Instructor: [0:00] The Dart toolbox contains utility methods for working with lists as part of the Dart core library. Use the forEach method to iterate over each element in a list.

[0:13] Use the map method to produce a new list after transforming each element in the given list. Calling the map method returns an iterable, which is the base type for Dart collections. Use the toList method to produce a list type.

[0:32] Use the contains method to check that a given element is in the list. Use the sort method to order items in a list based on the provided sorting function. Use the reduce method to compress the elements of a list into a single value. If you wish to provide an initial value, use the fold method instead.

[1:13] Use the every method to test whether all the items in a list satisfies the provided condition. To return a collection of elements that satisfies a particular test, use the where, firstWhere, or singleWhere methods.

[1:40] Here is how we use firstWhere and singleWhere. The firstWhere and singleWhere methods received a named argument called orElse. This allows you to define a callback that is invoked when no matches are found. Failure to define this named argument will throw a state error when no matches are found.

[2:13] You don't necessarily have to return null as you could return an item which is of the same type expected, in this case being a map. Use the take and skip methods to produce a collection while including or excluding elements. It's also possible to chain the take and skip methods.

[2:45] Use the expand method to expand or flatten a list into one or more elements. Here is another example duplicating the items of a list. Use the list.generate factory constructor to generate a list with a specified length.

[3:11] Generated lists by default are growable, which means that they can be extended. If you wish to disable this, set the growable option to false. Use the list.from factory constructor to clone the contents of a list. This concludes the lesson.