Dart Cheatsheet - Dart Syntax & Flutter Reference

All essential Dart commands organized by use case, with 43+ entries you can copy and run directly. Find the right command fast when you need it.

Languages·43 commands·Last updated 2026-07-21
Back to Languages

Variables & Types 10

var name = 'Dart';
type推导
String name = 'Dart';
显式type
final name = 'Dart';
最终variable(Run时constant)
const pi = 3.14;
Compile期constant
int? nullableInt;
可空type
late String lateVar;
延迟初始化
dynamic anything = 42;
动态type
List<int> numbers = [1, 2, 3];
list
Map<String, int> ages = {'Alice': 25};
map
Set<int> unique = {1, 2, 3};
set

Functions & Closures 6

int add(int a, int b) => a + b;
箭头function
void greet(String name, [String? title]) { }
optionalOptions
void greet({required String name, int age = 0}) { }
命名Options
Function fn = (x) => x * 2;
闭package
void Function(int) callback;
functiontype
typedef Comparator<T> = int Function(T a, T b);
type别名

Class & Mixin 7

class Person { String name; Person(this.name); }
class与构造function
class Student extends Person { }
inherit
class Flyable mixin Fly { }
mixin(mixin)
class Bird extends Animal with Flyable { }
使用mixin
abstract class Shape { double area(); }
抽象class
class Singleton { static final instance = Singleton._(); Singleton._(); }
单例模式
class Immutable { final int value; const Immutable(this.value); }
不可变class

Null Safety 6

String? nullable;
可空Declare
nullable?.length
安全call
nullable ?? 'default'
空merge操作符
nullable!
force非空断言
if (nullable != null) { nullable.length; }
type提升
late String lateVar;
延迟初始化(使用前必须赋值)

Async/Await 6

Future<int> fetchData() async { return 42; }
asyncfunction
int result = await fetchData();
awaitasync结果
Stream<int> countStream() async* { yield 1; }
async生成器
await for (var value in stream) { }
流监听
Future.wait([future1, future2]);
parallelawait多个 Future
Completer<int> completer = Completer();
手动控制 Future

Collections 8

list.where((x) => x > 5).toList()
Filter
list.map((x) => x * 2).toList()
map
list.reduce((a, b) => a + b)
归约
list.fold(0, (sum, x) => sum + x)
折叠
list.any((x) => x > 0)
是否exists
list.every((x) => x > 0)
是否全部满足
list.sort((a, b) => a.compareTo(b))
sort
list.groupBy((x) => x.category)
group(需Import collection package)

💡 Tips

  • Dart 的空安全是Compile期Check,尽量用 ? 和 ?? 避免Run时exception。
  • async/await 是async编程的核心,避免block UI 线程。
  • mixin 是 Dart 实现多inherit的方式,适合复用代码。

Official References

Commands are compiled from the official docs below. Click to verify the latest usage.

Maintained by LaoHand

Publicly updated on Jul 21, 2026, continuously proofread against official docs.

Found an error? Report it

Wrong command or description? Open an issue to help us fix it.

Found an error? Report it