Dart Language Learn note

2020-11-20

This is for myself memo.

A tour of the Dart language

https://dart.dev/guides/language/language-tour

Important concepts

  • Everything you can place in a variable is an object, and every object is an instance of a class. Even numbers, functions, and null are objects. All objects inherit from the Object class
  • Unlike Java, Dart doesn’t have the keywords publicprotected, and private. If an identifier starts with an underscore (_), it’s private to its library. For details, see Libraries and visibility.

Debug

  • Production code ignores the assert() call. During development, on the other hand, assert(condition) throws an exception if condition is false. For details, see Assert.

Final and const

  • If you never intend to change a variable, use final or const, either instead of var or in addition to a type.
  • A final variable can be set only once; a const variable is a compile-time constant. (Const variables are implicitly final.)
  • A final top-level or class variable is initialized the first time it’s used.
  • Instance variables can be final but not const.
  • Final instance variables must be initialized before the constructor body starts — at the variable declaration, by a constructor parameter, or in the constructor’s initializer list.

Operator

  • Here’s how the == operator works
    • If x or y is null, return true if both are null, and false if only one is null.
    • Return the result of the method invocation x.==(y). (That’s right, operators such as == are methods that are invoked on their first operand. For details, see Operators.)
    • Two strings are equivalent if they contain the same sequence of code units.
  • Type test operators
    • as: Typecast (also used to specify library prefixes)
    • is: True if the object has the specified type
    • is!: False if the object has the specified type
  • ??=: To assign only if the assigned-to variable is null.
  • ?.: The leftmost operand can be null; example: foo?.bar selects property bar from expression foo unless foo is null (in which case the value of foo?.bar is null)

Class

  • To get an object’s type at runtime, you can use Object’s runtimeType property, which returns a Type object.
    print('The type of a is ${a.runtimeType}');
  • the order of class initialization is as follows:
    1. initializer list
    2. superclass’s no-arg constructor
    3. main class’s no-arg constructor
  • The right-hand side of an initializer does not have access to this.
  • Initializer lists are handy when setting up final fields
  • Factory constructors
    • Use the factory keyword when implementing a constructor that doesn’t always create a new instance of its class.
    • Factory constructors have no access to this
  • Operators are instance methods with special names
    • Vector operator +(Vector v) => Vector(x + v.x, y + v.y);
  • Getters and setters are special methods that provide read and write access to an object’s properties
    • double get bottom => top + height;
    • set bottom(double value) => top = value - height;
    • Operators such as increment (++) work in the expected way, whether or not a getter is explicitly defined. To avoid any unexpected side effects, the operator calls the getter exactly once, saving its value in a temporary variable.
  • Every class implicitly defines an interface containing all the instance members of the class and of any interfaces it implements
  • A class implements one or more interfaces by declaring them in an implements clause and then providing the APIs required by the interfaces
    • class Point implements Comparable, Location {…}
  • Use extends to create a subclass, and super to refer to the superclass
    • use the @override annotation to indicate that you are intentionally overriding a member

Generators

Dart has built-in support for two kinds of generator functions:

Synchronous generator: Returns an Iterable object.

Iterable<int> naturalsTo(int n) sync* {
  int k = 0;
  while (k < n) yield k++;
}

Asynchronous generator: Returns a Stream object.

Stream<int> asynchronousNaturalsTo(int n) async* {
  int k = 0;
  while (k < n) yield k++;
}

Typedefs

typedef Compare = int Function(Object a, Object b);

class SortedCollection {
  Compare compare;

  SortedCollection(this.compare);
}
typedef Compare<T> = int Function(T a, T b);

int sort(int a, int b) => a - b;

void main() {
  assert(sort is Compare<int>); // True!
}

BookMark 2020/11/19: language-tour

Flutter

Posted by Bee