Dart Language Tour Learning Tips

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

String interpolation

printInteger(int aNumber) {
  print('The number is $aNumber.'); // Print to console.
}

Very handy when print some log. Will auto call variable toString() method.

Private variables

Private variables start with _, it will declared this variable in private in class or library.

import

import will exposed all element to current namespace(terrible and confusion), if library has same name element must be carefully.

Use prefix to solve this conflicting:

import 'package:lib1/lib1.dart';
import 'package:lib2/lib2.dart' as lib2;

// Uses Element from lib1.
Element element1 = Element();

// Uses Element from lib2.
lib2.Element element2 = lib2.Element();

dynamic type

Dart is strongly typed.

Every thing in dart is based on Object type so you can put any type into Object type variable.

Why we need dynamic type to save unknown type variable?

Different at:

The Object type variable is declared to Object type, even you put int or bool value in this variable, it still the Object type.

The dynamic clearly declared: Now I don’t known the current variable type but can infer type when get detail data.

void log(Object object) {
  print(object.toString());
}

/// Returns a Boolean representation for [arg], which must
/// be a String or bool.
bool convertToBool(dynamic arg) {
  if (arg is bool) return arg;
  if (arg is String) return arg == 'true';
  throw ArgumentError('Cannot convert $arg to a bool.');
}

null

Any variable can be null cause it is all base on Object.

I like this feature so much.

final or const

final can be set once, const is compile-time constant.