Flutter Cheat Sheet

This content was generated with the assistance of AI and should be verified against the official documentation before being relied on in production.

A compact reference for Flutter 3.47.9 (stable) with Dart 3.13.7 and the Trailhead example packages: go_router 18.1.4, flutter_riverpod 3.5.0, dio 5.11.1, drift 2.35.4, shared_preferences 2.5.6, camera 0.12.2, geolocator 14.1.2, json_serializable 6.15.2 and mocktail 1.0.5. For explanations and complete examples, follow the page links at the end.

Download the Flutter Cheat Sheet (PDF)

CLI Commands

Command Purpose

flutter doctor

Check the toolchain and report missing pieces.

flutter create trailhead

Create a new project.

flutter pub add dio

Add a dependency (use dev: prefix for dev dependencies).

flutter pub get

Fetch dependencies listed in pubspec.yaml.

flutter run -d <device-id>

Run on a device; press r for hot reload and R for hot restart.

flutter devices

List connected devices and emulators.

flutter analyze

Static analysis.

dart format .

Format all Dart files.

dart run build_runner build

Run code generators (json_serializable, drift).

Dart Syntax

Concept Example

Variables

final name = 'Ridge'; var count = 0; const pi = 3.14;

Null safety

String? note; note?.length; note ?? 'none'; note!

Functions

int add(int a, int b) => a + b;

Named parameters

void log({required String id, int level = 0}) {}

Collections

[1, 2]; {'a': 1}; {1, 2}

Collection if and for

[for (final t in trails) t.name, if (admin) 'all']

String interpolation

'${trail.name}: $count'

Classes

class Trail { const Trail(this.id); final String id; }

Async

Future<int> f() async { return await g(); }

Sealed and switch

switch (r) { Success(:final value) => value, Failure() => null }

Records

(String, int) pair = ('a', 1);

Common Widgets

Widget Use

MaterialApp, Scaffold, AppBar

App root, screen skeleton, top bar.

Text, Icon, Image.network

Content.

ElevatedButton, TextButton, IconButton, FloatingActionButton

Buttons.

TextField, Form, TextFormField

Input and validation.

ListView.builder, GridView.builder

Long lazy lists and grids.

Card, ListTile, Chip

Rows and tags.

Dialog, SnackBar, BottomSheet

Transient surfaces.

StatelessWidget, StatefulWidget, ConsumerWidget

Widget base classes.

Layout

Widget Behaviour

Row, Column

Lay children along an axis; use mainAxisAlignment and crossAxisAlignment.

Expanded, Flexible

Share the remaining space inside a Row or Column.

Stack, Positioned

Overlay children.

Padding, SizedBox, Center, Align

Spacing and alignment.

Container, DecoratedBox

Size, colour, border and shadow.

SingleChildScrollView, SafeArea

Scroll one child; avoid notches and system bars.

Constraints go down, sizes go up, parents set positions.

State

Tool Typical use

setState

Local state in a StatefulWidget.

ValueNotifier with ValueListenableBuilder

Small observable values.

Riverpod: ref.watch, ref.read, ref.listen

App state; Provider, NotifierProvider, AsyncNotifierProvider.

ProviderScope

Root of a Riverpod app; overrides go here in tests.

Routing with go_router

Call Meaning

context.go('/trails/42')

Navigate to a location, replacing the stack.

context.push('/log')

Push on the stack.

context.pop()

Go back.

state.pathParameters, state.uri.queryParameters

Read path and query values inside a route builder.

redirect

Guard that returns a new location or null.

StatefulShellRoute.indexedStack

Bottom tabs that each keep their own stack.

Networking and Storage

Task Code

GET with dio

final r = await dio.get<List<dynamic>>('/trails');

JSON decode

Trail.fromJson(json as Map<String, dynamic>)

Key-value store

final prefs = await SharedPreferences.getInstance();

Documents directory

await getApplicationDocumentsDirectory()

Local database

drift tables and watch() streams.

Testing

Command or API Purpose

flutter test

Run unit and widget tests.

flutter test --coverage

Write coverage/lcov.info.

flutter test --update-goldens

Refresh golden images.

flutter test integration_test

Run integration tests on a device.

testWidgets, tester.pumpWidget, tester.tap, tester.pumpAndSettle

Widget test building blocks.

find.text, find.byType, findsOneWidget

Finders and matchers.

ProviderScope with overrides

Inject fakes into a widget test.

Build and Release

Command Purpose

flutter build apk --release

Release APK.

flutter build appbundle --release

Android App Bundle for Google Play.

flutter build ipa --release

iOS archive for App Store Connect.

--dart-define=KEY=value, --dart-define-from-file=file.json

Compile-time configuration.

--obfuscate --split-debug-info=build/symbols

Obfuscate Dart code and keep symbols.

--analyze-size

Produce a size analysis.

version: 1.4.0+27

Version name and build number in pubspec.yaml.