Chapter 8. Collections of richer classes
This chapter covers
- Working with collections
- Building type-safe, general-purpose classes with generics
- Overloading class operators
In the previous two chapters, we discussed how you can use classes and interfaces in your Dart applications and libraries. In this final chapter on classes, we’ll look at how to make classes richer and even more flexible and descriptive by using some of their advanced features.
We’ll start by using the built-in collection classes, such as List and Map, which allow you to manipulate lists of data. By using generic types, you can help make accessing your collections type-safe and benefit from additional validations from the type checker. You’ll use the indexer syntax to access elements directly in lists and maps and discover how to build literal maps and lists from known, preexisting values. We’ll also look at the JavaScript Object Notation (JSON) methods to convert maps into strings and back to maps.
Next, we’ll examine how you can make your own classes available for developers to use in a generic fashion, so that rather than creating two nearly identical classes, you’ll be able to create a single class that can be used, strongly typed, in two ways.