# Dart

# Important Concepts

  1. Every variable is an object, and every object is an instance of a class.
    • numbers, functions, and null are objects.
    • all objects inherit from the Object class except for null.
  2. Dart is strongly typed by optional.
  3. Dart is null safe.
    1. If you want nullable add ? at the end. int? might be integer or null.
    2. Object? allows all objects
      1. Uninitialized variables have value of null.
      2. dynamic can be used to disable static checking. Not recommended.
    3. Object allows all objects except null.
  4. Dart allows generic types.
    1. List : list of ints
    2. List : list of objects of any type.
  5. main() is top level function.
  6. Dart also supports top-level variables.
  7. Identifier can start with __ or letter
  8. Dart doesn't have public', protected and private. If identifier started with __, it's private to its library.
  9. Dart has both expression and statement
    1. Expressions have runtime value : expression condition ? expr1 : expr2 which has either expr1 or expr2 as runtime value
    2. Statement has no value if else statement.
    3. Statement often contains one or more expressions, but an expression can't directly contain a statement.
  10. Dart has warnings and errors.
    1. Warnings tell you it might not work but executable
    2. Errors can be either compile time or run time. compile time error prevents executing, run time error raises exception
  11. Top-level and class variables are lazily initialized; the initialization code runs the first time the variable is used.
    1. if error you can use late to tell dart to run initializer the first time the variable is used.

# Overview

  1. Object oriented
    1. 코드를 어떻게 오브젝트에 정리할 것인지 관건이다.
  2. Statically typed
  3. C style syntax
  4. Multiple environment
    • 브라우저에서는 JS로 transpile, CL에서는 Dart VM으로 실행, 모바일에서는 머신코드로 컴파일 된다.

# Types

  • Every value has a type

  • Every variable has a type it can reference

    • Value vs Variable

      image-20220206103605309

    • Variable reference and pointed value must have same type

  • Once a variable has a type associated, the type cannot be changed

  • Dart can infer type : var type이 String인지 int인지 코드를 보고 유추한다.

  • Dart can be dynamic : type을 다이나믹하게 할 수 있다.

# Why use types?

  • Improved performance : 동적언어를 쓸때는 엔진이 체크를 하고 있다.
  • Easier on large projects
  • Less need of unit tests
  • Find simple errors

# Built-in types

  • Numbers (opens new window) (int, double)

    • int 64 bit number
    • double 64 bit floating point number.
    • both are subtypes of num and includes same basic operators.
    • converting to/from string to num
    // String -> int
    var one = int.parse('1');
    assert(one == 1);
    
    // String -> double
    var onePointOne = double.parse('1.1');
    assert(onePointOne == 1.1);
    
    // int -> String
    String oneAsString = 1.toString();
    assert(oneAsString == '1');
    
    // double -> String
    String piAsString = 3.14159.toStringAsFixed(2);
    assert(piAsString == '3.14');
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    • bit operation

      assert((3 << 1) == 6); // 0011 << 1 == 0110
      assert((3 | 4) == 7); // 0011 | 0100 == 0111
      assert((3 & 4) == 0); // 0011 & 0100 == 0000
      
      1
      2
      3

# Object Oriented Programming

  • 오브젝트는 fields, properties라고 부르는 데이터와 오브젝트에 정의된 methods라는 function으로 이루어진다.
  • 데이터는 오브젝트 외부에서는 가려져있고 메소드로만 접근할 수 있다.
  • Class는 청사진이고 실제로 작동하는 object는 instance라고 한다.