Collections in Dart

Collections teaches you to work with many values at once.

In this chapter, you will:

  • store many values in order with Lists
  • add, remove, search and sort items
  • change or question a whole list in one line with map, where and fold
  • learn Sets, Maps and Queues for unique values, key-value pairs and first-in-first-out work

What each lesson covers:

  • Lists: How to create lists, read and change items by index, add, remove, search and sort items, and use spread, collection if and collection for.
  • Working with Iterables: How to transform and question a list with map, where, fold, reduce, any, every, firstWhere, take and skip, and how to chain them.
  • Sets: How to keep unique values with sets and set operations, and how to add and remove at both ends with queues from dart:collection.
  • Maps: How to store key-value pairs, read and change values by key, handle missing keys, and add, remove, search, loop over and transform maps.

This chapter has 4 lessons. Plan on about 75 minutes, with 15 exercises to check what you learned. Every example is real Dart code: change it, run it again and compare the output, which is the fastest way to learn how each feature behaves.

Simple lesson: finish this chapter and you can handle a whole class of students, not just one.

Lessons in this section

  1. Lists

    How to create lists, read and change items by index, add, remove, search and sort items, and use spread, collection if and collection for.

    • Create lists and use indexes
    • Add, remove, search and sort items
    • Use spread, collection if, collection for and unmodifiable lists
  2. Working with Iterables

    How to transform and question a list with map, where, fold, reduce, any, every, firstWhere, take and skip, and how to chain them.

    • Explain what an Iterable is
    • Use map, where, fold and reduce
    • Use any, every, firstWhere, take and skip, and chain them
  3. Sets

    How to keep unique values with sets and set operations, and how to add and remove at both ends with queues from dart:collection.

    • Create sets and use add, remove, contains and set operations
    • Convert between lists and sets, and make unmodifiable sets
    • Use a Queue to add and remove at both ends
  4. Maps

    How to store key-value pairs, read and change values by key, handle missing keys, and add, remove, search, loop over and transform maps.

    • Create maps and use keys to read and write values
    • Handle missing keys safely
    • Add, remove, loop over and transform entries