npx skills add ...
npx skills add flutter/skills --skill dart-modern-features
npx skills add flutter/skills --skill dart-modern-features
Guidelines for using modern Dart features (v3.0 - v3.10) such as Records, Pattern Matching, Switch Expressions, Extension Types, Class Modifiers, Wildcards, Null-Aware Elements, and Dot Shorthands.
Use this skill when:
To find candidates for modernization:
Search for switch statements where every case assigns to the same variable or returns:
switch\s*\([^)]+\)\s*\{\s*caseSearch for manual map or JSON property extraction and type checking:
containsKey\(['"][^'"]+['"]\)json\[['"][^'"]+['"]\]\s+is\s+Search for collection if statements checking for null:
if\s*\(\w+\s*!=\s*null\)\s*\w+Search for long numbers without separators:
\b\d{6,}\b (Matches numbers with 6 or more digits).Use records as anonymous, immutable, aggregate structures to bundle multiple objects without defining a custom class. Prefer them for returning multiple values from a function or grouping related data temporarily.
Avoid: Creating a dedicated class for simple multiple-value returns.
Prefer: Using records to bundle types seamlessly on the fly.
Use patterns to destructure complex data into local variables and match against
specific shapes or values. Use them in switch, if-case, or variable
declarations to unpack data directly.
Avoid: Manually checking types, nulls, and keys for data extraction.
Prefer: Combining type-checking, validation, and assignment into a single statement.
Use switch expressions to return a value directly, eliminating bulky case and
break statements.
Avoid: Using switch statements where every branch simply returns or assigns a value.
Prefer:
Returning the evaluated expression directly using the => syntax.
Use class modifiers (sealed, final, base, interface) to restrict how
classes can be used outside their defines library. Prefer sealed for defining
closed families of subtypes to enable exhaustive checking.
Avoid:
Using open abstract classes when the set of subclasses is known and fixed.
Prefer:
Using sealed to guarantee to the compiler that all cases are covered.
Use extension types for a zero-cost wrapper around an existing type. Use them to restrict operations or add custom behavior without runtime overhead.
Avoid: Allocating new wrapper objects just for domain-specific logic or type safety.
Prefer: Using extension types which compile down to the underlying type at runtime.
Use underscores (_) in number literals strictly to improve visual readability
of large numeric values.
Avoid: Long number literals that are difficult to read at a glance.
Prefer: Using underscores to separate thousands or other groupings.
Use wildcards (_) as non-binding variables or parameters to explicitly signal
that a value is intentionally unused.
Avoid: Inventing clunky, distinct variable names to avoid "unused variable" warnings.
Prefer: Explicitly dropping the binding with an underscore.
Use null-aware elements (?) inside collection literals to conditionally
include items only if they evaluate to a non-null value.
Avoid:
Using collection if statements for simple null checks.
Prefer:
Using the ? prefix inline.
Use dot shorthands to omit the explicit type name when it can be confidently inferred from context, such as with enums or static fields.
Avoid: Fully qualifying type names when the type is obvious from the context.
Prefer: Reducing visual noise with inferred shorthand.
(String, int) fetchUser() {
return ('Alice', 42);
}
void main() {
var user = fetchUser();
print(user.$1); // Alice
}void processJson(Map<String, dynamic> json) {
if (json.containsKey('name') && json['name'] is String &&
json.containsKey('age') && json['age'] is int) {
String name = json['name'];
int age = json['age'];
print('$name is $age years old.');
}
}void processJson(Map<String, dynamic> json) {
if (json case {'name': String name, 'age': int age}) {
print('$name is $age years old.');
}
}String describeStatus(int code) {
switch (code) {
case 200:
return 'Success';
case 404:
return 'Not Found';
default:
return 'Unknown';
}
}String describeStatus(int code) => switch (code) {
200 => 'Success',
404 => 'Not Found',
_ => 'Unknown',
};abstract class Result {}
class Success extends Result {}
class Failure extends Result {}
String handle(Result r) {
if (r is Success) return 'OK';
if (r is Failure) return 'Error';
return 'Unknown';
}sealed class Result {}
class Success extends Result {}
class Failure extends Result {}
String handle(Result r) => switch(r) {
Success() => 'OK',
Failure() => 'Error',
};class Id {
final int value;
Id(this.value);
bool get isValid => value > 0;
}extension type Id(int value) {
bool get isValid => value > 0;
}const int oneMillion = 1000000;const int oneMillion = 1_000_000;void handleEvent(String ignoredName, int status) {
print('Status: $status');
}void handleEvent(String _, int status) {
print('Status: $status');
}var names = [
'Alice',
if (optionalName != null) optionalName,
'Charlie'
];var names = ['Alice', ?optionalName, 'Charlie'];LogLevel currentLevel = LogLevel.info;LogLevel currentLevel = .info;