Skip to content

Dart Language Tour

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 String
final age = 36; // assign-once int
const pi = 3.14159; // compile-time constant
int? 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 constant

const 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.

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, Ada
greeting(name: 'Ada', prefix: 'Hi'); // Hi, Ada
// Arrow syntax for single-expression functions
int 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.

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 .. 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.
What is the difference between `final` and `const` in Dart?
Why are named parameters so central to Flutter?
What does collection-if (`if (cond) widget`) inside a list do?
What does the cascade operator `..` do?