Introduction
With the release of Dart 3.0, Flutter developers gained access to a powerful feature called dot shorthands (also known as unnamed extensions). This syntax improvement allows us to write more concise and readable code by eliminating repetitive type names when the context makes them clear.
In this guide, we’ll explore how dot shorthands work, when to use them, and best practices for implementing them in our Flutter projects.
What Are Dot Shorthands?
Dot shorthands allow you to omit the class or enum name when creating instances or accessing constants, as long as Dart can infer the type from context. Instead of writing the full qualified name, you can start directly with a dot.
Example:
Common Use Cases in Flutter
Dot shorthands are particularly useful in Flutter for several common scenarios:
1. Colors
Container( color: .red, // Colors.red child: Icon(Icons.star, color: .white), )
2. EdgeInsets & Padding
Padding(
padding: .all(16), // EdgeInsets.all(16)
// or
padding: .symmetric(horizontal: 24, vertical: 12),
child: Text('Hello'),
)
3. Alignment
Align(
alignment: .centerRight, // Alignment.centerRight
child: Text('Aligned'),
)
4. MainAxisAlignment & CrossAxisAlignment
Row( mainAxisAlignment: .spaceBetween, crossAxisAlignment: .center, children: [...], )
5. BorderRadius
Container(
decoration: BoxDecoration(
borderRadius: .circular(12), // BorderRadius.circular(12)
),
)
6. TextStyle Properties
Text(
'Bold Text',
style: TextStyle(
fontWeight: .bold, // FontWeight.bold
fontSize: 18,
),
)
Complete Example
Here’s a real-world example showing the difference:
Without Dot Shorthands:
Container(
padding: EdgeInsets.all(16),
alignment: Alignment.center,
decoration: BoxDecoration(
color: Colors.blue,
borderRadius: BorderRadius.circular(8),
),
child: Text(
'Hello Flutter',
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold,
fontSize: 18,
),
),
)
With Dot Shorthands:
Container(
padding: .all(16),
alignment: .center,
decoration: BoxDecoration(
color: .blue,
borderRadius: .circular(8),
),
child: Text(
'Hello Flutter',
style: TextStyle(
color: .white,
fontWeight: .bold,
fontSize: 18,
),
),
)
Constructor Tear-offs
Another powerful feature enabled by Dart 3.0 is constructor tear-offs. This allows you to reference constructors directly without creating wrapper functions:
Before:
children: items.map((item) => Text(item)).toList()
After:
children: items.map(Text.new).toList()
When to Use Dot Shorthands
✅ Do use dot shorthands when:
- The type is clear from context (parameter type, variable type, etc.)
- It improves code readability by reducing visual noise
- Working with common Flutter classes (Colors, EdgeInsets, Alignment, etc.)
❌ Don’t use dot shorthands when:
- The context is ambiguous or unclear
- It makes code harder to understand for team members
- Working with less common or custom types where explicitness aids comprehension
Requirements
To use dot shorthands in your Flutter project:
- Ensure your project uses Dart 3.0 or later
- Update your pubspec.yaml:
environment: sdk: '>=3.0.0 <4.0.0'
Benefits
- Reduced Verbosity: Less typing and visual clutter in your code
- Improved Readability: Focus on what matters rather than repetitive type names
- Type Safety: Still fully type-safe as Dart infers the correct type
- Modern Syntax: Aligns with contemporary programming language trends
Best Practices for Our Team
- Be Consistent: If you start using dot shorthands in a file, use them throughout that file for similar cases
- Prioritize Clarity: When in doubt, favor explicit type names over brevity
- Gradual Adoption: You don’t need to refactor all existing code immediately. Adopt dot shorthands in new code and when touching existing files
- IDE Support: Modern IDEs (Android Studio, VS Code) provide full autocomplete and navigation support for dot shorthands
- Code Reviews: Ensure reviewers are familiar with this syntax to avoid confusion during PR reviews
Common Patterns Reference
Here’s a quick reference for the most common dot shorthand patterns:
| Category | Traditional | Shorthand |
|---|---|---|
| Colors | Colors.blue |
.blue |
| EdgeInsets | EdgeInsets.all(16) |
.all(16) |
| Alignment | Alignment.center |
.center |
| BorderRadius | BorderRadius.circular(8) |
.circular(8) |
| FontWeight | FontWeight.bold |
.bold |
| BoxFit | BoxFit.cover |
.cover |
| MainAxisAlignment | MainAxisAlignment.start |
.start |
| CrossAxisAlignment | CrossAxisAlignment.end |
.end |
Conclusion
Dot shorthands are a simple yet powerful feature that can make your Flutter code more concise and readable. By eliminating repetitive type names where context makes them clear, we can focus on the logic and structure of our applications.
As we adopt this modern syntax in our projects, remember that clarity should always be the priority. Use dot shorthands where they improve readability, but don’t hesitate to use explicit type names when they make the code clearer.
Happy coding!
Additional Resources
- Official Dart 3.0 announcement and documentation
- Flutter API documentation for commonly used classes
- Team coding standards and style guide