Dart Reference
v1.0.0Quick 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 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 s = 'hello'; // or "hello"
UTF-16 string with single or double quotes. Supports interpolation with $ and multi-line with triple quotes.
bool flag = true;
Boolean type with only two values: true and false. No truthy/falsy coercion like JavaScript.
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 value = 'hello';
Disables static type checking. Can hold any value and change type at runtime. Use sparingly.
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.
int? maybeNull = null;
Append ? to any type to allow null. Without ?, the variable must always hold a non-null value.
int value = nullable!;
Assert a nullable expression is non-null at runtime. Throws if null. Use only when you are certain.
obj?.property
Access a member only if the receiver is non-null. Returns null if receiver is null.
expr1 ?? expr2
Returns expr1 if non-null, otherwise expr2. Useful for providing default values.
variable ??= value;
Assigns value only if the variable is currently null. Leaves existing non-null values untouched.
late String name;
Deferred initialization for non-nullable variables. Checked at first access, not at declaration.
List<T> / var list = [1, 2, 3];
Ordered, indexed collection (array). Growable by default. Supports generics.
Map<K, V> / var m = {'key': 'value'};
Key-value pairs. Keys are unique. Literal syntax uses curly braces with colon separators.
Set<T> / var s = {1, 2, 3};
Unordered collection of unique elements. Literal syntax resembles Map but without colons.
[...list1, ...list2]
Spread elements of a collection into another. Use ...? for nullable collections.
[if (cond) item] / [for (var x in list) x]
Build collections conditionally or with iteration directly inside the literal.
.map() .where() .fold() .any() .every()
Lazy transformations on iterables. Chain map, where, fold, reduce for functional-style processing.
class Name { Type field; }
Class definition with typed fields, constructors, and methods. All classes implicitly extend Object.
ClassName(this.field1, this.field2);
Dart shorthand assigns constructor parameters directly to fields using this.field syntax.
ClassName.name(params) : super()
Alternative constructors with descriptive names. Useful for multiple creation patterns.
factory ClassName() { return instance; }
Factory constructor can return existing instances, subtypes, or cached objects instead of always creating new.
mixin Name on Base { methods }
Reusable behavior mixed into classes with 'with' keyword. Can require a superclass using 'on'.
extension Name on Type { methods }
Add methods to existing types without modifying them. Dart 2.7+ feature.
Future<T>
Represents a value or error available at some point in the future. Core of Dart async programming.
Future<T> fn() async { await expr; }
Mark function async to use await. Await pauses execution until the Future completes.
Stream<T>
Asynchronous sequence of events. Listen with await for or .listen(). Single-subscription or broadcast.
Stream<T> fn() async* { yield value; }
Generator function that produces a Stream. yield emits values, yield* delegates to another stream.
FutureBuilder<T>(future: f, builder: fn)
Flutter widget that rebuilds its UI based on Future state: waiting, done, or error.
Isolate.spawn(entryPoint, message)
Lightweight concurrent worker with separate memory. Communicate via SendPort/ReceivePort.
class W extends StatelessWidget { Widget build(ctx) }
Immutable widget — build() is called once per configuration. Use for static UI elements.
class W extends StatefulWidget { State createState() }
Widget with mutable state. Separate widget and State class. Call setState() to trigger rebuild.
Container(padding, margin, decoration, child)
Convenience widget combining common painting, positioning, and sizing operations.
Column(children: []) / Row(children: [])
Flex layout — Column is vertical, Row is horizontal. Control alignment and spacing with properties.
ListView.builder(itemCount, itemBuilder)
Lazily-built scrollable list. Only renders visible items — efficient for long lists.
Navigator.push(context, MaterialPageRoute(..))
Stack-based navigation between screens. Push adds a route, pop removes the top route.
if (cond) { } else if (cond) { } else { }
Standard conditional branching. Dart 3 also supports if-case for pattern matching.
for (var i = 0; i < n; i++) { } / for (var x in list) { }
C-style for loop and for-in iteration over any Iterable.
while (cond) { } / do { } while (cond);
while checks condition first. do-while executes body at least once before checking.
var x = switch (val) { pattern => expr, _ => def };
Dart 3 switch expressions return values directly. Exhaustive matching with pattern support.
if (value case Pattern(:var field)) { }
Destructure objects, records, and collections with patterns. Powerful Dart 3 feature.
case pattern when condition:
Add a boolean guard to a pattern in switch statements or if-case expressions.
name: app\ndependencies:\n http: ^1.0.0
Project manifest defining name, version, dependencies, and dart/flutter SDK constraints.
dart pub get
Download and resolve all dependencies listed in pubspec.yaml. Creates pubspec.lock.
import 'package:pkg/file.dart';
Import a library from a package, Dart SDK, or relative path.
import 'pkg' show A, B; / import 'pkg' hide C;
Selectively import or exclude names from a library. Reduces namespace pollution.
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>
Add a dependency to pubspec.yaml and run pub get in one command.