ApelMerah

Learning Dart [Basic]

Aug 9th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 3.03 KB | None | 0 0
  1. /* Basic Dart
  2.  * https://www.tutorialspoint.com/dart_programming/dart_programming_functions.htm
  3.  * https://dartpad.dartlang.org/
  4.  */
  5.  
  6. void main() {
  7.   int _int, __int;
  8.   String _string;
  9.  
  10.   /*
  11.    * Data Type
  12.    * 1. Numbers
  13.    *    - int
  14.    *    - double
  15.    * 2. Strings
  16.    *    - Runes (UTF-32)
  17.    *    -   String (UTF-16)
  18.    * 3. Boolean
  19.    *    - bool
  20.    * 4. List & Map
  21.    *    - List (ordered group of objects)
  22.    *    - Map (represent set of values as key-value pairs)
  23.    * 5. Dynamic Type
  24.    *    - dynamic (if data type not specified || explicitly used)
  25.    */
  26.  
  27.   /*
  28.    * const = compile-time constant (Implicitly final)
  29.    */
  30.  
  31.   // ~/ division (return int)
  32.   print("/ = ");
  33.   print(25 / 4);
  34.   print("");
  35.   print("~/ = ");
  36.   print(25 ~/ 4);
  37.   print("");
  38.  
  39.   /*
  40.    * is = true if object has specified type
  41.    * is! = false if object has specified type
  42.    */
  43.   _int = 5;
  44.   if (_int is int) {
  45.     print("_int is int");
  46.   }
  47.  
  48.   if (_int is! String) {
  49.     print("_int is not String");
  50.   }
  51.   print("");
  52.  
  53.   /*
  54.    * expr1 ?? expr2
  55.    * (if expr1 is !NULL then return expr1 else return expr2)
  56.    */
  57.   _int = null;
  58.   __int = 5;
  59.   print(_int ?? __int);
  60.   print("");
  61.  
  62.   /*
  63.    * Parsing
  64.    *    - parse()
  65.    * parse a string into a number
  66.    */
  67.   _string = "123";
  68.   print(num.parse(_string) + 32);
  69.   print("");
  70.  
  71.   /*
  72.    * String
  73.    *    - Single line vs Multi line
  74.    */
  75.   _string = 'Single line';
  76.   print("Current String = ${_string}");
  77.   _string = "Single line";
  78.   print("Current String = ${_string}");
  79.   _string = '''Multi
  80.  line''';
  81.   print("Current String = ${_string}");
  82.   _string = """Multi
  83.     line""";
  84.   print("Current String = ${_string}");
  85.  
  86.   print("");
  87.  
  88.   /*
  89.    * List
  90.    *    - Fixed Length
  91.    *    - Growable
  92.    */
  93.   List listAnimals = new List(5);
  94.   listAnimals[0] = "Dog";
  95.   listAnimals[1] = "Cat";
  96.   listAnimals[2] = "Sheep";
  97.   listAnimals[3] = "Lizard";
  98.   listAnimals[4] = "Bear";
  99.   print("List of Animals = ${listAnimals}");
  100.  
  101.   List listDogs = new List();
  102.   listDogs.add("Beagle");
  103.   listDogs.add("Pitbull");
  104.   listDogs.add("Bull Dog");
  105.   listDogs.add("Chihuahua");
  106.   listDogs.remove("Bull Dog");
  107.   listDogs.removeAt(0);
  108.   print("List of Dogs = ${listDogs}");
  109.   print("");
  110.  
  111.   /*
  112.    * Maps
  113.    */
  114.   Map profile = new Map();
  115.   profile = {'Name': 'John Doe', 'Password': 'jonjon'};
  116.   profile['Description'] = 'Looks like a dead body';
  117.   print("Profile: ${profile['Description']}");
  118.   print("");
  119.  
  120.   /*
  121.    * Symbol
  122.    */
  123.  
  124.   /*
  125.    * Runes
  126.    */
  127.   Runes _runes = new Runes('\u{1f605}');
  128.   print("Runes: ${new String.fromCharCodes(_runes)}");
  129.  
  130.   /*
  131.    * Function
  132.    *    - [] = optional parameter
  133.    *    - {} = optional named parameter
  134.    */
  135.   foo("Hello");
  136.   optionalNamed(myString: "World");
  137.   optionalNamed();
  138.   // Lambada Function / Arrow Function
  139.   lambadaFun();
  140. }
  141.  
  142. foo(String _string, [int _int]) {
  143.   print(_string);
  144.   print(_int);
  145. }
  146.  
  147. optionalNamed({String myString : "Default Value"}) {
  148.   print(myString);
  149. }
  150.  
  151. lambadaFun() => print("This is lambada");
Add Comment
Please, Sign In to add comment