dart-effective-style
dart-style-guide
Goal
Applies idiomatic Dart styling, naming conventions, and formatting rules to ensure consistent, readable, and maintainable code across Dart and Flutter projects. Assumes a standard Dart SDK environment where the dart CLI is available.
Instructions
-
Apply Naming Conventions Ensure all identifiers match Dart's official style guidelines. Use the Decision Logic section below to determine the correct casing for any given identifier.
-
Order Directives Organize all imports and exports at the top of the file in the following strict order, separated by blank lines, and sorted alphabetically within each group:
dart:imports.package:imports.- Relative imports.
exportdirectives.
import 'dart:async'; import 'dart:collection'; import 'package:bar/bar.dart'; import 'package:foo/foo.dart'; import 'util.dart'; export 'src/error.dart'; -
Format Code Always format the code using the official Dart formatter. If the formatter produces hard-to-read code, refactor the code (e.g., extract variables) to be more formatter-friendly.
dart format . -
Enforce Flow Control Braces Always use curly braces for flow control statements to prevent the dangling else problem.
// GOOD if (isWeekDay) { print('Bike to work!'); } else { print('Go dancing or read a book!'); } // ACCEPTABLE (Single line, no else) if (arg == null) return defaultValue; -
Handle Unused Callback Parameters Use wildcards (
_) for unused callback parameters to signal intent.futureOfVoid.then((_) { print('Operation complete.'); }); // Multiple unused parameters .onError((_, __) { print('Operation failed.'); });
Decision Logic
When creating or renaming an identifier, follow this flowchart logic:
- Is it a Class, Enum, Typedef, Extension, or Type Parameter?
- Yes: Use
UpperCamelCase.class SliderMenu {} typedef Predicate<T> = bool Function(T value); extension SmartIterable<T> on Iterable<T> {}
- Yes: Use
- Is it a Package, Directory, Source File, or Import Prefix?
- Yes: Use
lowercase_with_underscores.import 'dart:math' as math; import 'package:js/js.dart' as js; // File: slider_menu.dart
- Yes: Use
- Is it a Constant Variable or Enum Value?
- Yes: Use
lowerCamelCase(Do NOT useSCREAMING_CAPSunless matching legacy code).const pi = 3.14; const defaultTimeout = 1000;
- Yes: Use
- Is it a Variable, Parameter, Named Parameter, Class Member, or Top-Level Definition?
- Yes: Use
lowerCamelCase.var count = 3; HttpRequest httpRequest;
- Yes: Use
- Does the name contain an Acronym or Abbreviation?
- Is it longer than two letters? Capitalize it like a normal word (e.g.,
Http,Uri). - Is it exactly two letters? Keep both capitalized (e.g.,
ID,TV) unless it is the start of alowerCamelCaseidentifier, in which case both are lowercase (e.g.,idToken,tvSet).
- Is it longer than two letters? Capitalize it like a normal word (e.g.,
- Is the member or top-level declaration meant to be private to its library?
- Yes: Prefix it with a leading underscore
_. - No: Do NOT use a leading underscore. (Never use leading underscores for local variables or parameters).
- Yes: Prefix it with a leading underscore
Constraints
- Mandatory Formatting: DO run
dart formatas a mandatory step before committing or finalizing any code changes. - Import Preferences: PREFER relative imports for files located inside the
libdirectory of the same package. - No Prefix Letters: DON'T use prefix letters (like Hungarian notation). Use
defaultTimeout, notkDefaultTimeout. - No Explicit Library Names: DON'T explicitly name libraries (e.g., avoid
library my_library;). Uselibrary;if a directive is needed for annotations. - Line Length: PREFER lines 80 characters or fewer. STOP AND ASK THE USER: if a specific string or configuration requires exceeding the 80-character limit and cannot be reasonably refactored.
- Related Skills:
dart-static-analysis,dart-api-design.