Skip to main content

Dart Reference

v1.0.0

Quick reference for Dart language syntax, types, and Flutter patterns

Quick reference for Dart 3.x and Flutter — null safety, async/await, collections, pattern matching, and widget patterns.

How to use
  • Search for syntax like "Future", "mixin", or "null" to find relevant entries instantly.
  • Click any card to expand a runnable code example — copy and adapt it in your Dart or Flutter project.
  • Use category tabs (e.g. Null Safety, Async/Await, Flutter Widgets) to browse related concepts.
  • Try the Example picker (Simple → Advanced → Pro) to pre-fill a search for common patterns.

48 entries found

int & double
Data Types

int x = 42; double y = 3.14;

Integer and double-precision floating-point numeric types. int is 64-bit on native, arbitrary precision on web.

String
Data Types

String s = 'hello'; // or "hello"

UTF-16 string with single or double quotes. Supports interpolation with $ and multi-line with triple quotes.

bool
Data Types

bool flag = true;

Boolean type with only two values: true and false. No truthy/falsy coercion like JavaScript.

var, final, const
Data Types

var x = 1; final y = 2; const z = 3;

var infers type and is mutable. final is runtime-immutable. const is compile-time constant.

dynamic
Data Types

dynamic value = 'hello';

Disables static type checking. Can hold any value and change type at runtime. Use sparingly.

typedef
Data Types

typedef Compare<T> = int Function(T a, T b);

Type alias for function types and complex types. Dart 2.13+ supports non-function type aliases.

Nullable type (T?)
Null Safety

int? maybeNull = null;

Append ? to any type to allow null. Without ?, the variable must always hold a non-null value.

Null assertion (!)
Null Safety

int value = nullable!;

Assert a nullable expression is non-null at runtime. Throws if null. Use only when you are certain.

Null-aware access (?.)
Null Safety

obj?.property

Access a member only if the receiver is non-null. Returns null if receiver is null.

Null coalescing (??)
Null Safety

expr1 ?? expr2

Returns expr1 if non-null, otherwise expr2. Useful for providing default values.

Null-aware assignment (??=)
Null Safety

variable ??= value;

Assigns value only if the variable is currently null. Leaves existing non-null values untouched.

late keyword
Null Safety

late String name;

Deferred initialization for non-nullable variables. Checked at first access, not at declaration.

List
Collections

List<T> / var list = [1, 2, 3];

Ordered, indexed collection (array). Growable by default. Supports generics.

Map
Collections

Map<K, V> / var m = {'key': 'value'};

Key-value pairs. Keys are unique. Literal syntax uses curly braces with colon separators.

Set
Collections

Set<T> / var s = {1, 2, 3};

Unordered collection of unique elements. Literal syntax resembles Map but without colons.

Spread operator (...)
Collections

[...list1, ...list2]

Spread elements of a collection into another. Use ...? for nullable collections.

Collection if / for
Collections

[if (cond) item] / [for (var x in list) x]

Build collections conditionally or with iteration directly inside the literal.

Iterable methods
Collections

.map() .where() .fold() .any() .every()

Lazy transformations on iterables. Chain map, where, fold, reduce for functional-style processing.

class
Classes & Mixins

class Name { Type field; }

Class definition with typed fields, constructors, and methods. All classes implicitly extend Object.

Constructor shorthand
Classes & Mixins

ClassName(this.field1, this.field2);

Dart shorthand assigns constructor parameters directly to fields using this.field syntax.

Named constructor
Classes & Mixins

ClassName.name(params) : super()

Alternative constructors with descriptive names. Useful for multiple creation patterns.

factory constructor
Classes & Mixins

factory ClassName() { return instance; }

Factory constructor can return existing instances, subtypes, or cached objects instead of always creating new.

mixin
Classes & Mixins

mixin Name on Base { methods }

Reusable behavior mixed into classes with 'with' keyword. Can require a superclass using 'on'.

extension
Classes & Mixins

extension Name on Type { methods }

Add methods to existing types without modifying them. Dart 2.7+ feature.

Future
Async/Await

Future<T>

Represents a value or error available at some point in the future. Core of Dart async programming.

async / await
Async/Await

Future<T> fn() async { await expr; }

Mark function async to use await. Await pauses execution until the Future completes.

Stream
Async/Await

Stream<T>

Asynchronous sequence of events. Listen with await for or .listen(). Single-subscription or broadcast.

async* / yield
Async/Await

Stream<T> fn() async* { yield value; }

Generator function that produces a Stream. yield emits values, yield* delegates to another stream.

FutureBuilder (Flutter)
Async/Await

FutureBuilder<T>(future: f, builder: fn)

Flutter widget that rebuilds its UI based on Future state: waiting, done, or error.

Isolate
Async/Await

Isolate.spawn(entryPoint, message)

Lightweight concurrent worker with separate memory. Communicate via SendPort/ReceivePort.

StatelessWidget
Flutter Widgets

class W extends StatelessWidget { Widget build(ctx) }

Immutable widget — build() is called once per configuration. Use for static UI elements.

StatefulWidget
Flutter Widgets

class W extends StatefulWidget { State createState() }

Widget with mutable state. Separate widget and State class. Call setState() to trigger rebuild.

Container & Padding
Flutter Widgets

Container(padding, margin, decoration, child)

Convenience widget combining common painting, positioning, and sizing operations.

Column & Row
Flutter Widgets

Column(children: []) / Row(children: [])

Flex layout — Column is vertical, Row is horizontal. Control alignment and spacing with properties.

ListView.builder
Flutter Widgets

ListView.builder(itemCount, itemBuilder)

Lazily-built scrollable list. Only renders visible items — efficient for long lists.

Navigator.push
Flutter Widgets

Navigator.push(context, MaterialPageRoute(..))

Stack-based navigation between screens. Push adds a route, pop removes the top route.

if / else
Control Flow

if (cond) { } else if (cond) { } else { }

Standard conditional branching. Dart 3 also supports if-case for pattern matching.

for & for-in
Control Flow

for (var i = 0; i < n; i++) { } / for (var x in list) { }

C-style for loop and for-in iteration over any Iterable.

while & do-while
Control Flow

while (cond) { } / do { } while (cond);

while checks condition first. do-while executes body at least once before checking.

switch expression (Dart 3)
Control Flow

var x = switch (val) { pattern => expr, _ => def };

Dart 3 switch expressions return values directly. Exhaustive matching with pattern support.

Pattern matching (Dart 3)
Control Flow

if (value case Pattern(:var field)) { }

Destructure objects, records, and collections with patterns. Powerful Dart 3 feature.

Guard clause (when)
Control Flow

case pattern when condition:

Add a boolean guard to a pattern in switch statements or if-case expressions.

pubspec.yaml
Package Management

name: app\ndependencies:\n http: ^1.0.0

Project manifest defining name, version, dependencies, and dart/flutter SDK constraints.

dart pub get
Package Management

dart pub get

Download and resolve all dependencies listed in pubspec.yaml. Creates pubspec.lock.

import
Package Management

import 'package:pkg/file.dart';

Import a library from a package, Dart SDK, or relative path.

show / hide
Package Management

import 'pkg' show A, B; / import 'pkg' hide C;

Selectively import or exclude names from a library. Reduces namespace pollution.

part & part of
Package Management

part 'file.dart'; / part of 'lib.dart';

Split a single library across multiple files. Both files share the same private scope.

dart pub add
Package Management

dart pub add <package>

Add a dependency to pubspec.yaml and run pub get in one command.