applying-effective-dart
Skill7 starsChanged 8 months agoMITView on GitHub
Dart 3 modern features (through 3.10) and Effective Dart best practices including records, patterns, sealed classes, dot shorthands, null-aware collection elements, and wildcard variables. Use when adopting Dart 3.7+ syntax, using dot shorthands for enums or static members, using null-aware elements in lists or maps, using wildcard _ parameters, improving code quality, or reviewing Dart code conventions.
In this repository41 other files it gives its agents.
- .github/copilot-instructions.mdCopilot instructions
- skill-creatorSkill
- flutter-dbSkill
- flutter-routingSkill
- flutter-state-managementSkill
- configuring-codemagicSkill
- deploying-with-fastlaneSkill
- integrating-firebaseSkill
- implementing-fl-chartSkill
- integrating-google-adsSkill
- animating-flutter-widgetsSkill
- routing-with-autorouteSkill
- implementing-flutter-blocSkill
- configuring-deep-linksSkill
- persisting-data-with-driftSkill
- building-flutter-production-appsSkill
- generating-flutter-uiSkill
- routing-with-gorouterSkill
- managing-hive-storageSkill
- using-flutter-hooksSkill
- running-flutter-isolatesSkill
- managing-flutter-provider-stateSkill
- implementing-responsive-layoutsSkill
- implementing-riverpodSkill
- managing-secure-storageSkill
- flutter-setupSkill
- managing-shared-preferencesSkill
- implementing-flutter-social-authSkill
- flutter-testingSkill
- using-fpdartSkill
- generating-freezed-modelsSkill
- configuring-github-actionsSkill
- building-jaspr-web-appsSkill
- testing-with-marionetteSkill
- implementing-openapi-in-dartSkill
- integrating-revenuecatSkill
- integrating-sentrySkill
- developing-serverpod-backendSkill
- building-shadcn-flutter-uiSkill
- integrating-supabaseSkill
- converting-ts-to-dartSkill
The file
Show its rules and text
We split this skill into the rules it gives an agent
17 rules. About 0 are common, 2 are distinctive, and 15 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈2
Distinctive · estimate
This author's own choices. Worth a look.
15
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 20
Multiple Return Values: Prefer Records over throwaway wrapper classes.
- Distinctive: line 21
Named Fields: Use named fields when returning more than two values or when semantics are ambiguous.
- Very specific: line 33
Replace complex if-statements: Use if-case to validate shape and extract variables simultaneously.
- Very specific: line 120
Formatting: Use dart format and enable core or recommended lints in analysis_options.yaml.
- Very specific: line 130
Type Inference: Use var/final for local variables.
- Very specific: line 132
late: Use only when initialization must be deferred. - Very specific: line 135
Arrow Functions: Use => for single-expression functions.
- Very specific: line 136
Named Parameters: Prefer named parameters for multiple optional arguments.
- Very specific: line 137
Async: Prefer async/await over .then()/.catchError() chains.
- Very specific: line 141
Avoid
!: Use type promotion (if (value != null)) or pattern matching instead of forced unwrapping. - Very specific: line 142
Null-aware collection elements: Prefer [?nullable] over [if (x != null) x] for cleaner collection literals (Dart 3.8+).
- Very specific: line 148
Minimal exposure: Only expose members that require external access.
- Very specific: line 149
Getters/Setters: Use get for property-like reads with no side effects or expensive computation.
- Very specific: line 150
Constructors: Use named constructors (e.g., User.fromJson) and redirecting constructors for semantics.
- Very specific: line 160
Use Records for composite returns instead of one-off data classes.
- Very specific: line 162
Use dot shorthands (.value) when the context type is unambiguous — reduces enum and static verbosity.
- Very specific: line 163
Use ?element in collection literals instead of if (x != null) x — cleaner and more idiomatic (Dart 3.8+).
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Dart 3 Latest Features and Effective Dart Best Practices Guide
Goal
Covers Dart 3 features (through 3.10) and Effective Dart practices for writing safe, maintainable code.
Instructions
1. Dart 3 Features
1.1 Records
Records aggregate multiple typed values without requiring a dedicated class.
- Best Practices:
- Multiple Return Values: Prefer Records over throwaway wrapper classes.
- Named Fields: Use named fields when returning more than two values or when semantics are ambiguous.
// ✅ Recommended Usage ({double lat, double lon}) getLocation(String city) { return (lat: 25.0330, lon: 121.5654); }
1.2 Patterns and Destructuring
Patterns match and destructure data from Records, Lists, Maps, and custom objects.
- Best Practices:
- Destructuring on declaration: Extract values from Records or Lists directly at assignment.
- Replace complex if-statements: Use
if-caseto validate shape and extract variables simultaneously.
// ✅ Recommended Usage final json = {'user': ['Zack', 25]}; if (json case {'user': [String name, int age]}) { print('User $name is $age years old.'); }
1.3 switch Expressions and Exhaustive Checking
switch expressions return values and provide compile-time exhaustiveness for sealed classes and enums.
- Best Practices:
- Use as expression: Assign diverging values concisely with switch expressions.
- Omit
default: On sealed/enum types, omitdefaultso the compiler catches unhandled cases when new variants are added.
1.4 Class Modifiers
Dart 3 class modifiers control inheritance and implementation boundaries.
- Best Practices:
sealed: For Algebraic Data Types (ADTs). Enables exhaustive switch checking. Ideal for State or Result/Error types.interface: Allowsimplementsonly; preventsextends.final: Prevents bothextendsandimplementsfrom external libraries.
1.5 Wildcard Variables — Dart 3.7
Local variables and parameters named _ are now non-binding — they can be declared multiple times without name collision, and their value cannot be read.
// Multiple _ params in constructors — no collision
Foo(_, this._, super._) {}
// Discard unused callback args cleanly
list.where((_) => true);
// Multiple discards in the same scope
var _ = expensiveCall1();
var _ = expensiveCall2(); // OK — no shadowing errorTop-level names, field names, and class names called _ are unchanged — this only applies to local variables and parameters.
1.6 Null-Aware Collection Elements — Dart 3.8
Prefix a nullable expression with ? inside a collection literal to omit it when null. Works in lists, sets, and maps.
String? lunch = isTuesday ? 'tacos' : null;
int? count = hasItems ? items.length : null;
// ❌ Before 3.8
var old = [if (lunch != null) lunch, if (count != null) count];
// ✅ Dart 3.8+
var menu = [?lunch, ?count]; // nulls are silently excluded
// Works in maps too
final headers = <String, String>{
'Content-Type': 'application/json',
if (token != null) 'Authorization': 'Bearer $token', // old style
?'X-Custom': customHeader, // ✅ null-aware
};1.7 Dot Shorthands — Dart 3.10
Omit the type name when accessing a static member or constructor in a context where the expected type is already known.
// ❌ Before 3.10
Color color = Color.blue;
Widget w = Padding(padding: EdgeInsets.all(16), child: ...);
crossAxisAlignment: CrossAxisAlignment.start,
// ✅ Dart 3.10+
Color color = .blue;
Widget w = Padding(padding: .all(16), child: ...); // EdgeInsets inferred
crossAxisAlignment: .start,
mainAxisSize: .min,
// Works in switch cases
switch (color) {
case .blue: print('blue');
case .red: print('red');
}Dot shorthands work for: enum values, static fields, static methods, and named constructors — anywhere the context type is known.
2. Effective Dart: Style
Consistent style enables collaboration.
- Formatting: Use
dart formatand enablecoreorrecommendedlints inanalysis_options.yaml. - Naming Conventions:
- Classes, Enums, Typedefs, Type parameters:
UpperCamelCase - Libraries, Packages, Directories, Files:
lowercase_with_underscores - Variables, Parameters, Functions, Methods:
lowerCamelCase - Constants:
lowerCamelCase(e.g.,const defaultTimeout = 1000;) — not SCREAMING_CAPS.
- Classes, Enums, Typedefs, Type parameters:
3. Effective Dart: Usage
3.1 Variables and Types
- Type Inference: Use
var/finalfor local variables; omit type annotations where the compiler infers correctly. - Collections: Initialize with literals (
var list = [];). Use spread operators and collectionif/forinstead of.add()calls. late: Use only when initialization must be deferred. Never access before assignment.
3.2 Functions and Async
- Arrow Functions: Use
=>for single-expression functions. - Named Parameters: Prefer named parameters for multiple optional arguments; apply
requiredas needed. - Async: Prefer
async/awaitover.then()/.catchError()chains.
3.3 Null Safety
- Minimize nullables: Design variables as non-nullable where possible.
- Avoid
!: Use type promotion (if (value != null)) or pattern matching instead of forced unwrapping. - Null-aware collection elements: Prefer
[?nullable]over[if (x != null) x]for cleaner collection literals (Dart 3.8+).
3.4 Async Annotations — Dart 3.9
@awaitNotRequired: Annotate aFuture-returning method to suppressunawaited_futureslint at the call site when intentionally fire-and-forget.
4. Effective Dart: API Design
- Minimal exposure: Only expose members that require external access. Prefix private members with
_. - Getters/Setters: Use
getfor property-like reads with no side effects or expensive computation. Don't add setters for every property — preferfinalconstructor initialization. - Constructors: Use named constructors (e.g.,
User.fromJson) and redirecting constructors for semantics. Provideconstconstructors wherever possible.
5. Recommended Lints (Dart 3.9+)
| Lint | Purpose |
switch_on_type | Prefer switch over is-chain if for type checks |
unnecessary_unawaited | Flag unawaited() calls where the future is already non-async |
Constraints
- Use
Recordsfor composite returns instead of one-off data classes. - Omit type annotations where the compiler can infer the type safely.
- Use dot shorthands (
.value) when the context type is unambiguous — reduces enum and static verbosity. - Use
?elementin collection literals instead ofif (x != null) x— cleaner and more idiomatic (Dart 3.8+).
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

