Darts: Darts 101 for Flutter

Fat Arrow: "=>" is "{}"

Dynamic Vs Object: Semantic Difference for accepting any type, difference is Hashcode is property on Object and Dynamic is when system cannot properly express any type that dev want to use.

Print to console with "print()"

Quick Guide to Darts

Constructor:

  • Used to create object
  • new keyword is optional in Dart2
  • Declaring Constructor by creating function with same name as class/ syntactic constructor
    • class Point {
        num x, y;
      
        Point(num x, num y) {
          // There's a better way to do this, stay tuned.
          this.x = x;
          this.y = y;
        }
      }
    • class Point {
        num x, y;
      
        Point(this.x, this.y);
      }
    • Named Constructor, class name with additional identifier
      • Gives extra clarity when implement multiple constructor within a class
      • Constructor are not inherited, subclass do not inherit superclass's named constructor
    • class Point {
        num x, y;
      
        Point(this.x, this.y);
      
        // Named constructor
        Point.origin() {
          x = 0;
          y = 0;
        }
      }
    • Default Constructor (No constructor declared): No argument constructor in superclass 
Getting Object type at Runtime

print('The type of a is ${a.runtimeType}');
Initialiser List
From Stackoverflow
  DetailScreen({Key key, @required this.todo}) : super(key: key);
The constructor has two named parameters.
Named parameters are optional by default.
@required is an annotation recognized by the Dart analyzer and produces a warning if not passed when invoked at build time (it has no effect at run time).
: starts the "initializer list", a comma sparated list of expressions executed before the constructors of the super classes and therefore also before the contructors body.
It is often used to check parameter values using assertions and to initialize final fields with calculated values.
A limitation is, that expressions can't read-access this. (implicitely or explicitely) because the object initialization is not completed before the super constructors are executed.
The last element in the initializer is an implicit call to the default constructor of the super class if omitted, or the call to a specific constructor of the current class or the super class if given.
In the example in your question the key parameter passed to the constructor is forwarded to the named parameter key of the unnamed constructor of the super class.

This is an example to supplement Günter Zöchbauer's explanation. It is the constructor of the Alignwidget.
class Align extends SingleChildRenderObjectWidget {

  // constructor
  const Align({
    Key key,                                                   // named parameter
    this.alignment = Alignment.center,                         // named parameter
    this.widthFactor,                                          // named parameter
    this.heightFactor,                                         // named parameter
    Widget child                                               // named parameter
  }) : assert(alignment != null),                              // initializer list
       assert(widthFactor == null || widthFactor >= 0.0),      // initializer list
       assert(heightFactor == null || heightFactor >= 0.0),    // initializer list
       super(key: key, child: child);                          // initializer list

  // class variables
  final AlignmentGeometry alignment;
  final double widthFactor;
  final double heightFactor;
More notes:
  • parameters without the this. prefix are variables of the superclass.
  • parameters that do begin with this. are variables defined in the current class.

Comments

Popular posts from this blog

Setting up Terminal in Android Studio for Flutter and Bash Profile (For Mac)

Flutter Widget 101 Part 1 (Basic of Flutters, Layouts, Rows and Columns)

Flutter Widget 101 Part 4 (Navigation and Drawers)