Page 19 - Dart Language
P. 19

Types for the key and value can also be defined using generics:


         var nobleGases = new Map<int, String>();
         var nobleGases = <int, String>{};


        Maps can otherwise be created using the map literal:


         var map = {
             "key1": "value1",
             "key2": "value2"
         };



        Map each element in the collection.


        All collection objects contain a map method that takes a Function as an argument, which must take
        a single argument. This returns an Iterable backed by the collection. When the Iterable is
        iterated, each step calls the function with a new element of the collection, and the result of the call
        becomes the next element of the iteration.

        You can turn an Iterable into a collection again by using the Iterable.toSet() or Iterable.toList()
        methods, or by using a collection constructor which takes an iterable like Queue.from or List.from.


        Example:


         main() {
           var cats = [
             'Abyssinian',
             'Scottish Fold',
             'Domestic Shorthair'
           ];

           print(cats); // [Abyssinian, Scottish Fold, Domestic Shorthair]

           var catsInReverse =
           cats.map((String cat) {
             return new String.fromCharCodes(cat.codeUnits.reversed);
           })
           .toList(); // [nainissybA, dloF hsittocS, riahtrohS citsemoD]

           print(catsInReverse);
         }


        See dartpad example here: https://dartpad.dartlang.org/a18367ff767f172b34ff03c7008a6fa1


        Filter a list


        Dart allows to easily filter a list using where.


         var fruits = ['apples', 'oranges', 'bananas'];
         fruits.where((f) => f.startsWith('a')).toList(); //apples


        Of course you can use some AND or OR operators in your where clause.



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