Page 18 - Dart Language
P. 18

Chapter 4: Collections




        Examples



        Creating a new List


        Lists can be created in multiple ways.


        The recommended way is to use a List literal:


         var vegetables = ['broccoli', 'cabbage'];


        The List constructor can be used as well:


         var fruits = new List();


        If you prefer stronger typing, you can also supply a type parameter in one of the following ways:


         var fruits = <String>['apples', 'oranges'];
         var fruits = new List<String>();


        For creating a small growable list, either empty or containing some known initial values, the literal
        form is preferred. There are specialized constructors for other kinds of lists:


         var fixedLengthList1 = new List(8);
         var fixedLengthList2 = new List.filled(8, "initial text");
         var computedValues = new List.generate(8, (n) => "x" * n);
         var fromIterable = new List<String>.from(computedValues.getRange(2, 5));


        See also the Effective Dart style guide about collections.

        Creating a new Set


        Sets can be created via the constructor:


         var ingredients = new Set();
         ingredients.addAll(['gold', 'titanium', 'xenon']);


        Creating a new Map


        Maps can be created in multiple ways.


        Using the constructor, you can create a new map as follow:


         var searchTerms = new Map();





        https://riptutorial.com/                                                                               13
   13   14   15   16   17   18   19   20   21   22   23