Flutter code is full of repeated type names: MainAxisAlignment.center, EdgeInsets.all(16), Alignment.topLeft. Dart's dot shorthands feature (stable since Dart 3.10) lets you drop the type name when the compiler already knows the expected type. This tutorial shows where it helps, where it doesn't, and how to adopt it across a team.
Prerequisites
- Dart 3.10 or newer, and a matching Flutter stable release.
- Your
pubspec.yamlSDK constraint must besdk: ^3.10.0or higher, because the language version gates the syntax.
Check your toolchain:
flutter --version
dart --version
The basics
When the context type is known, write .value instead of Type.value.
// Before
Column(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.start,
children: [ /* ... */ ],
)
// After
Column(
mainAxisAlignment: .center,
crossAxisAlignment: .start,
children: [ /* ... */ ],
)
It also works for static members and constructors of the context type:
Padding(
padding: .all(16),
child: Container(
alignment: .topLeft,
decoration: BoxDecoration(borderRadius: .circular(12)),
),
)
Here .all(16) resolves to EdgeInsetsGeometry's expected type, and .circular(12) resolves against BorderRadiusGeometry. Because the parameter type is a supertype, the shorthand works when the member is declared on that type or accessible through it. If the compiler cannot find the member, you get a clear error and can fall back to the full name.
Where it shines
- Enums in widget parameters. Alignment, axis, text style and fit values read naturally.
- Equality and comparisons.
if (status == .loading)works when the left side has a known type. - Switch patterns on enums.
String label(UploadState s) => switch (s) {
.idle => 'Ready',
.uploading => 'Uploading…',
.done => 'Complete',
.failed => 'Try again',
};
Where to avoid it
- Inferred contexts. With
var x = .center;there is no context type, so it fails. Declare the type. - Overloaded meaning.
.zerocould beDuration.zero,Offset.zeroorEdgeInsets.zero. In a long argument list the reader may not know which. Keep full names when the parameter name does not make the type obvious. - Code review diffs. Mixed styles in one file make diffs noisy. Pick a convention.
- Generics with wide types. Parameters typed
Objectordynamicgive no context to resolve against.
A readable rule of thumb
Use shorthand when the parameter name states the type's meaning (mainAxisAlignment, textAlign, fit). Use the full name when the value stands alone, such as final padding = EdgeInsets.all(8); or an argument whose name is generic like value:.
Rolling it out
- Raise the SDK constraint in
pubspec.yamland runflutter pub get. - Apply the automated fix. Dart ships a lint,
prefer_double_quotes-style, for many styles; checkdart fix --listfor the current shorthand-related fix in your SDK version and preview with:
dart fix --dry-run
- Decide your policy in
analysis_options.yamland document it. If your SDK version offers a dot-shorthand lint, enable it only after the team agrees on the rule. - Migrate by feature folder in small pull requests, not one repo-wide commit, so reviewers can still read the diffs and
git blamestays useful. - Run your widget and golden tests. The change is purely syntactic, so golden images should not change. If they do, you have found a real bug.
Testing that nothing changed
flutter analyze
flutter test
Because shorthands compile to the same code, a green analyzer and unchanged goldens are strong evidence the migration is safe.
Team conventions we recommend
- Shorthand for enum-like arguments with descriptive parameter names.
- Full names for
Duration,Offset,Colorand other types with manyzero/transparent-style members. - Full names in public API examples and documentation, so readers can copy them anywhere.
- Do not mix styles within a single widget's argument list.
Summary
Dot shorthands trim noise from Flutter build methods, but they trade explicitness for brevity. Adopt them with a written convention, migrate incrementally, and let your analyzer and golden tests confirm nothing changed. If your team needs help planning a Dart or Flutter upgrade across several apps, get in touch.