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.
CLI Commands
| Command | Purpose |
|---|---|
|
Check the toolchain and report missing pieces. |
|
Create a new project. |
|
Add a dependency (use |
|
Fetch dependencies listed in |
|
Run on a device; press |
|
List connected devices and emulators. |
|
Static analysis. |
|
Format all Dart files. |
|
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 |
|---|---|
|
App root, screen skeleton, top bar. |
|
Content. |
|
Buttons. |
|
Input and validation. |
|
Long lazy lists and grids. |
|
Rows and tags. |
|
Transient surfaces. |
|
Widget base classes. |
Layout
| Widget | Behaviour |
|---|---|
|
Lay children along an axis; use |
|
Share the remaining space inside a |
|
Overlay children. |
|
Spacing and alignment. |
|
Size, colour, border and shadow. |
|
Scroll one child; avoid notches and system bars. |
Constraints go down, sizes go up, parents set positions.
State
| Tool | Typical use |
|---|---|
|
Local state in a |
|
Small observable values. |
Riverpod: |
App state; |
|
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. |
|
Go back. |
|
Read path and query values inside a route builder. |
|
Guard that returns a new location or |
|
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 |
|
Local database |
drift tables and |
Testing
| Command or API | Purpose |
|---|---|
|
Run unit and widget tests. |
|
Write |
|
Refresh golden images. |
|
Run integration tests on a device. |
|
Widget test building blocks. |
|
Finders and matchers. |
|
Inject fakes into a widget test. |
Build and Release
| Command | Purpose |
|---|---|
|
Release APK. |
|
Android App Bundle for Google Play. |
|
iOS archive for App Store Connect. |
|
Compile-time configuration. |
|
Obfuscate Dart code and keep symbols. |
|
Produce a size analysis. |
|
Version name and build number in |
All Pages
Foundations — Dart Language Essentials, Getting Started with Flutter, and Architecture and Rendering.
UI and state — Widgets and Layout, State Management, and Navigation and Routing.
Data and platform — Data, Storage and Networking and Platform Integration.
Quality and delivery — Testing and Building and Releasing.
Related — Hybrid Apps, React Native, Kotlin Multiplatform, and Kotlin Multiplatform and Flutter.