Posts

Class

  class with a constructor class Fruit { String type ; // Constructor - Same name as class Fruit( this . type ) ; } void main () { Fruit fruit = Fruit ( 'apple' ) ; print(fruit. type ) ; } named parameters class Fruit { String type ; // Constructor - Same name as class Fruit({ this . type }) ; //new } void main () { Fruit fruit = Fruit (type: 'Apple' ) ; //new print(fruit. type ) ; } class that does not define a constructor class BaristaNoConstructor { String name ; int experience ; } void main () { BaristaNoConstructor baristaNoConstructor = BaristaNoConstructor() ; baristaNoConstructor.experience = 10 ; print( 'baristaNoConstructor.experience: ${baristaNoConstructor.experience} ' ) ; // baristaNoConstructor.experience: 10 } class that defines a constructor with named parameters class BaristaWithConstructor { String name ; int experience ; // Constructor - Named parameters by using { } BaristaWithConstructor({ this . name , this

Strings

Method .toUpperCase()  var s = 'string interpolation' ; print( 'That deserves all caps. ' + ' ${s.toUpperCase()} is very handy!' ) ; // 'That deserves all caps. STRING INTERPOLATION is very handy!'

Maps

  A Map can be created by Map<String , String> gifts = const { 'first' : 'paper' , 'second' : 'cotton' , 'third' : 'leather' } var gifts = { // Key: Value 'first' : 'partridge' , 'second' : 'turtledoves' , 'fifth' : 'golden rings' } ; var gifts = Map () ; gifts[ 'first' ] = 'partridge' ; gifts[ 'second' ] = 'turtledoves' ; gifts[ 'fifth' ] = 'golden rings' ; var nobleGases = { 2 : 'helium' , 10 : 'neon' , 18 : 'argon' , } ; var nobleGases = Map () ; nobleGases[ 2 ] = 'helium' ; nobleGases[ 10 ] = 'neon' ; nobleGases[ 18 ] = 'argon' ;

Lists

 A list can be created by List<int> list = [ 1 , 2 , 3 ] ; var list = [ 1 , 2 , 3 ] ; spread operator  ( ... ) var list ; var list2 = [ 0 , ...?list] ; if the list is null, need to use  null-aware spread operator  ( ...? ) var list ; var list2 = [ 0 , ...list] ; print(list2) ;  //[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The getter 'iterator' was called on null.    for-in var collection = [ 1 , 2 , 3 ] ; for ( var x in collection) { print(x) ; // 1 2 3 } Method .length var list = [ 1 , 2 , 3 ] ; print(list. length ) ; //3 .forEach() void printElement(int element) { print(element) ; } var list = [ 1 , 2 , 3 ] ; // Pass printElement as a parameter. list.forEach(printElement) ; .indexOf() var list = [ 'apples' , 'bananas' , 'oranges' ] ; list.forEach((item) { print( ' ${list.indexOf(item)} : $item ' ) ; }) ; // 0: apples // 1: bananas // 2: oranges arrow notation: list.forEach(

Functions

  String say(String from , String msg , [String device = 'carrier pigeon' , String mood]) { var result = ' $from says $msg ' ; if (device != null ) { result = ' $result with a $device ' ; } if (mood != null ) { result = ' $result (in a $mood mood)' ; } return result ; } print(say( 'Bob' , 'Howdy' ) ; //Bob says Howdy with a carrier pigeon print(say( 'Bob' , 'Howdy' , 'IPHONE' )) ; //Bob says Howdy with a IPHONE print(say( 'Bob' , 'Howdy' , 'IPHONE' , 'happy' )) ; //Bob says Howdy with a IPHONE (in a happy mood) Conditional expressions String playerName(String name) => name ?? 'Guest' ; print(playerName( 'Tom' )) ; //Tom print(playerName( null )) ; //Guest

Data types

 Null var gifts = { 'first' : 'partridge' }; assert ( gifts [ 'fifth' ] == null ); NaN // Check for NaN. var iMeantToDoThis = 0 / 0 ; assert ( iMeantToDoThis . isNaN );

Set

  Set will ignore items already in the set var halogens = { 'fluorine' , 'chlorine' , 'bromine' , 'iodine' , 'astatine' } ; var elements = <String>{} ; elements.add( 'fluorine' ) ; elements.addAll(halogens) ; print(elements) ;