The .foo dot shorthand syntax lets you omit the type name. As long as the compiler can infer the target type from context, you can write cleaner Dart code. When accessing enum values, static members or constructors, you don’t need to write the full Type.foo.
Put simply: dot shorthand allows expressions to start with the forms below, and you can chain further operations afterwards:
- Identifier
.myValue - Constructor
.new() - Constant creation
const .myValue()
Examples
enum Status { none, running, stopped, paused }
class Point {
final int x, y;
Point(this.x, this.y);
Point.origin() : x = 0, y = 0;
}
void main() {
// Named constructor shorthand
Point origin = .origin(); // Equivalent to Point.origin()
// Enum shorthand
Status currentStatus = .running; // Equivalent to Status.running
// Static method shorthand
int port = .parse('8080'); // Equivalent to int.parse('8080')
}Code language: Dart (dart)
Context type
Dot shorthands rely on the context type for the compiler to infer the corresponding type member.
Context type: The type Dart expects at a given position based on where the expression sits.
Example: Status currentStatus = .running
The compiler knows the variable requires type Status, so it resolves .running to Status.running.
Lexical structure & syntax
A static member shorthand is an expression starting with .. When context makes the type clear, you can use it to access static members, constructors and enum constants concisely.
Applicable scenarios
1. Enums
Enums work excellently with shorthands in assignments and switch matching, as the type is unambiguous:
enum LogLevel { debug, info, warning, error }
String colorCode(LogLevel level) {
return switch (level) {
.debug => 'gray',
.info => 'blue',
.warning => 'orange',
.error => 'red',
};
}
void main() {
// Function call
String warnColor = colorCode(.warning); // Automatically inferred as LogLevel.warning
}Code language: Dart (dart)
2. Named / Factory constructors
Supplies type arguments for generic class constructors:
class Point {
final double x, y;
const Point(this.x, this.y);
const Point.origin() : x = 0, y = 0;
factory Point.fromList(List<double> list) => Point(list[0], list[1]);
}
void main() {
Point origin = .origin();
Point p1 = .fromList([1.0, 2.0]);
// Generic class
List<int> intList = .filled(5, 0); // List.filled(5,0)
}Code language: Dart (dart)
3. No‑argument constructor .new
The .new shorthand invokes a class’s default unnamed constructor. It’s handy when initializing many state fields and automatically infers generic parameters:
// Before shorthand
late final AnimationController _animationController = AnimationController(vsync: this);
final ScrollController _scrollController = ScrollController();
// After shorthand
late final AnimationController _animationController = .new(vsync: this);
final ScrollController _scrollController = .new();
final GlobalKey<ScaffoldMessengerState> scaffoldKey = .new();
Map<String, Map<String, bool>> properties = .new();Code language: Dart (dart)
4. Static members (static methods, static fields)
int httpPort = .parse('80'); // int.parse('80')
BigInt bigIntZero = .zero; // BigInt.zeroCode language: Dart (dart)
5. Constant expressions
Shorthands are usable in constant contexts as long as the target member is a compile‑time constant:
enum Status { none, running }
class Point {
final double x, y;
const Point(this.x, this.y);
const Point.origin() : x = 0.0, y = 0.0;
}
const Status defaultStatus = .running;
const Point myOrigin = .origin();
const List<Point> keyPoints = [.origin(), .new(1.0, 1.0)];Code language: Dart (dart)
Method chaining
You can chain instance methods or properties after a shorthand. The compiler resolves the shorthand target from context first; subsequent chained calls must return compatible types:
String lowerH = .fromCharCode(72).toLowerCase();
// String.fromCharCode(72).toLowerCase()
print(lowerH); // hCode language: JavaScript (javascript)
void main() {
String lowerH = .fromCharCode(72).toLowerCase();
// String.fromCharCode(72).toLowerCase()
print(lowerH); // h
}Code language: Dart (dart)
D:\dartdemo\firstdart>dart run
Building package executable...
Built firstdart:firstdart.
h
Code language: CSS (css)
Dot shorthands