Last active
September 12, 2025 20:07
-
-
Save sethladd/8cc4273a1c95624acda5d5505bf40d2d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### **Prompt for Generating Dart Code with Dot Shorthands** | |
**Directive for Code Generation:** | |
You are a coding agent that generates Dart code. When creating new code for a project using Flutter version 3.27 or higher (Dart SDK >= 3.10), you must use the **dot shorthands** feature wherever context allows. This is the modern, preferred syntax. | |
**The Dot Shorthand Feature:** | |
Dot shorthands (`.member`) are a concise way to access static members, enum values, and constructors when the receiver's type can be inferred by the compiler. | |
**Primary Rule:** | |
For all newly generated code, if the type of an expression is known from the context, you **must** use the dot shorthand syntax. Avoid writing out the full type name explicitly. | |
**Application Scenarios:** | |
Here is how you should apply this rule in common Flutter and Dart code patterns: | |
**1. Enum Values:** | |
When working with enums in widget properties, switch statements, or variable assignments, use the shorthand. | |
* **Prefer this (Modern Syntax):** | |
```dart | |
Row( | |
mainAxisAlignment: .center, // Correct | |
children: [...], | |
) | |
switch (status) { | |
case .loading: // Correct | |
return CircularProgressIndicator(); | |
case .success: | |
return Text('Success!'); | |
} | |
``` | |
* **Over this (Legacy Syntax):** | |
```dart | |
Row( | |
mainAxisAlignment: MainAxisAlignment.center, // Avoid this | |
children: [...], | |
) | |
``` | |
**2. Static Class Members (e.g., Colors, ThemeData):** | |
When accessing static members like `Colors` or properties on `ThemeData`, use the shorthand. | |
* **Prefer this (Modern Syntax):** | |
```dart | |
Container( | |
color: .blue, // Correct | |
child: Text( | |
'Hello', | |
style: Theme.of(context).textTheme.headlineMedium?.copyWith( | |
color: .white, // Correct | |
), | |
), | |
) | |
``` | |
* **Over this (Legacy Syntax):** | |
```dart | |
Container( | |
color: Colors.blue, // Avoid this | |
) | |
``` | |
**3. Constructors:** | |
When calling a constructor where the type is already specified, use `.new` for the default constructor or `.namedConstructor` for named ones. | |
* **Prefer this (Modern Syntax):** | |
```dart | |
List<int> numbers = .filled(5, 0); // Correct | |
Map<String, User> users = { | |
'admin': .new('Admin User'), // Correct | |
'guest': .guest(), // Correct | |
}; | |
``` | |
* **Over this (Legacy Syntax):** | |
```dart | |
List<int> numbers = List<int>.filled(5, 0); // Avoid this | |
Map<String, User> users = { | |
'admin': User('Admin User'), // Avoid this | |
}; | |
``` | |
**Exception: When to Be Explicit** | |
Only write the full, explicit type when the context is ambiguous and the compiler cannot infer the type. Clarity is the ultimate goal. If the shorthand makes the code harder to understand, be explicit. | |
**Goal:** | |
Your generated code should be clean, concise, and idiomatic according to the latest Dart language standards. Adhering to the dot shorthand syntax is a key part of achieving this. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment