Naming Conventions
// Classes, enums, typedefs — UpperCamelCase:
class UserProfile {}
enum AccountStatus { active, inactive }
typedef EventHandler = void Function(String);
// Variables, functions, parameters — lowerCamelCase:
var userName = 'Alice';
int calculateTotal(int price, int quantity) => price * quantity;
// Constants — lowerCamelCase (not SCREAMING_SNAKE):
const maxRetries = 3;
const defaultTimeout = Duration(seconds: 30);
// Private members — prefix with underscore:
class _InternalHelper {}
int _privateCounter = 0;
// Libraries and packages — lowercase_with_underscores:
// my_library.dart, user_service.dart
// Booleans — use positive names:
bool isActive = true; // ✅
bool hasPermission = true; // ✅
bool notDisabled = true; // ❌ (negative name)
# Format all files:
dart format .
# Check without modifying:
dart format --output=none --set-exit-if-changed .
# Dart uses 2-space indentation
# Max line length: 80 characters (soft limit)
# Trailing commas in multi-line collections preserve formatting:
// Good — trailing commas keep items on separate lines after formatting:
var config = Config(
host: 'localhost',
port: 5432,
database: 'mydb',
ssl: true, // ← trailing comma
);
// Good — short enough to be on one line:
var point = Point(x: 10, y: 20);
Effective Dart Guidelines
// PREFER final for variables that don't change:
final userName = 'Alice'; // ✅
var userName = 'Alice'; // ❌ (misleads — looks mutable)
// USE type inference — don't add obvious types:
var count = 0; // ✅
int count = 0; // ❌ (redundant)
List<User> users = []; // ✅ (type adds info)
// AVOID using dynamic:
dynamic value = 'text'; // ❌
Object? value = 'text'; // ✅ (type-safe)
// PREFER expression bodies for simple functions:
String greet(String name) => 'Hello, $name!'; // ✅
String greet(String name) { return 'Hello, $name!'; } // ❌
// USE ?? for null defaults:
String display = value ?? 'N/A'; // ✅
String display = value != null ? value : 'N/A'; // ❌
// USE collection if/for:
var items = [
'base',
if (isAdmin) 'admin-panel',
for (var tag in tags) 'tag:$tag',
];
Lints and Analysis
# analysis_options.yaml
include: package:lints/recommended.yaml
linter:
rules:
prefer_final_locals: true
avoid_print: true
prefer_const_constructors: true
sort_constructors_first: true
# Run analysis:
dart analyze
🧠 Quiz
1. What naming convention do Dart classes use?
- A) snake_case
- B) SCREAMING_SNAKE_CASE
- C) UpperCamelCase ✅
- D) lower-kebab-case
2. What does dart format . do?
- A) Checks for errors
- B) Automatically formats all Dart files in the directory ✅
- C) Compiles the project
- D) Generates documentation
Summary
Follow Effective Dart: use UpperCamelCase for types, lowerCamelCase for variables and functions, and _prefix for private members. Prefer final over var, use type inference, and write expression bodies for simple functions. Format with dart format and check with dart analyze. Add an analysis_options.yaml with recommended lints.