Dart Language Tour
Variables and inference
Section titled “Variables and inference”Dart is statically typed with strong inference. Use var and let Dart infer, or annotate when it aids clarity. final means assign-once; const means a compile-time constant.
var name = 'Ada'; // inferred Stringfinal age = 36; // assign-once intconst pi = 3.14159; // compile-time constantint? maybe; // nullable (null safety — next lesson)
// `final` vs `const`: final is runtime-fixed, const is compile-time-fixed.final now = DateTime.now(); // OK — computed at runtime// const now = DateTime.now(); // ERROR — not a compile-time constantconst matters enormously in Flutter: a const value is canonicalized — identical const objects are the same instance — which is what lets Flutter skip rebuilding const widgets.
Functions and named parameters
Section titled “Functions and named parameters”Dart functions support positional and named parameters. Flutter’s entire API is built on named parameters — every widget constructor is a wall of them.
// Named parameters in {}, `required` marks the mandatory ones, others have defaults.Widget greeting({required String name, String prefix = 'Hello'}) { return Text('$prefix, $name');}
greeting(name: 'Ada'); // Hello, Adagreeting(name: 'Ada', prefix: 'Hi'); // Hi, Ada
// Arrow syntax for single-expression functionsint square(int x) => x * x;This is why reading Flutter code means reading Widget(child: ..., padding: ..., color: ...) — named parameters make big constructors readable and order-independent.
Collections and collection-if / for
Section titled “Collections and collection-if / for”Lists, sets, and maps, plus collection-if and collection-for — used constantly to build widget lists conditionally.
final nums = [1, 2, 3];final set = {1, 2, 3};final map = {'a': 1, 'b': 2};
// Spread, collection-if, collection-for — you'll use these inside widget children:final children = [ const Header(), ...items, // spread if (isLoggedIn) const Profile(), // conditional element for (final n in nums) Text('$n'), // generated elements];That if/for-inside-a-list pattern is idiomatic Flutter — it replaces the awkward “build a list then push to it” you might reach for in other languages.
The cascade operator
Section titled “The cascade operator”The cascade .. runs several operations on the same object without repeating it — common in configuration-style code.
final paint = Paint() ..color = const Color(0xFF027DFD) ..strokeWidth = 4 ..style = PaintingStyle.stroke;// Each `..` returns the same Paint, so this configures one object.