concept list in category dart

This is an excerpt from Manning's book Dart in Action.
Use specific types (for example, String, List, and int) where doing so adds documentary value, such as for function parameters, return types, and public class members; but use var or final without type annotations where it doesn’t, such as inside function bodies. The Dart style guide available at www.dartlang.org recommends this approach. You should get used to seeing a mix of code like this, because it’s the way Dart was intended to be written.
In some instances you want to treat a specific variable as an instance of another type. A classic case of this is where you have a Collection variable, but you’re instantiating it as a list. This is valid, but no add() method is defined on the base Collection interface.
In order to use the Collection typed variable as a list, you need to use the as keyword. This lets the type checker know that you intend for the Collection variable to contain a List instance, which lets you use the variable as a list:
(permissionsCollection as List).add(... some permission ...);We’ll discuss the specific differences between the Collection and List interfaces a little later in the chapter. For now, add an extra field to your User from chapter 7 that will contain a collection of permissions, as shown in figure 8.3. When Alice logs on, her permissions are added to the collection.
The Collection interface, combined with the Iterable and Iterator interfaces, is a powerful construct, but you can’t create an instance of a Collection. An interface, remember, is a way of describing the methods and properties that should appear on a class that implements that interface. The core Dart libraries provide a number of implementation classes for the Collection interface: List, Queue, and Set. These classes provide different specializations of the Collection interface. Figure 8.5 shows the Collection interface and how it’s related to its children (List, Queue, and Set) and its parent (Iterable).
Figure 8.5. Collection is the core interface in Dart for dealing with collections of objects, but you need to use a concrete instance of List, Queue, or Set.
![]()
Dart doesn’t have an Array type, but as mentioned earlier, it does have List. Lists are dual purpose, being either fixed-size or extendable. If a list is fixed-size, you can insert elements only into predefined slots by using the square bracket index [] operator. An extendable list, on the other hand, can also have items added to and removed from it, effectively adding and removing slots in the list.
The final built-in generic type that we’ll look at in this chapter is Map<K,V>. You can use it to store lists of key/value pairs; you access the values using a key as an indexer. This is similar to the way you access values in a list by using a numeric indexer, such as myList[1], except that you can specify a nonnumeric indexer (typically a String). The generic type placeholders K and V represent the Key type and the Value type, respectively—they don’t need to be the same, and as with the other generic types we’ve looked at previously, they can be ignored (in which case you get a Map<dynamic, dynamic>).
When Alice logs on to the system, you can retrieve her User object. You can do the same for Bob and Charlie, but perhaps retrieving the User object from an enterprise system is a time-consuming exercise. You can implement a simple cache by storing a list of username strings and their associated user objects in a map. Because the Map interface has a default implementation class, you can create and manipulate the map as shown in the following listing.
You can create maps just like lists, with a predefined list of known keys and values. The map literal uses {} to define the map, and it contains a comma-separated list of keys and values, as shown in figure 8.7.