1 Conventions Cheatsheet
1.0.1 File Naming
Dart uses lowercase with underscores for file names, similar to Python:
my_widget.dart✅MyWidget.dart❌myWidget.dart❌
1.0.2 Class Naming
Classes use UpperCamelCase (PascalCase), just like in Python and JavaScript:
class UserProfile { } // ✅
class HTTPResponse { } // ✅ (acronyms stay uppercase)
class userProfile { } // ❌
class Http_Response { } // ❌1.0.3 Variable and Function Naming
Variables, functions, and parameters use lowerCamelCase:
// Variables
var itemCount = 0; // ✅
var item_count = 0; // ❌ (unlike Python)
// Functions
void calculateTotal() { } // ✅
void calculate_total() { } // ❌
// Parameters
void setUser({String firstName, int userId}) { } // ✅1.0.4 Constants
Constants use lowerCamelCase (different from Python’s UPPER_SNAKE_CASE):
const pi = 3.14159; // ✅
const defaultTimeout = 5000; // ✅
const DEFAULT_TIMEOUT = 5000; // ❌ (not Dart style)1.0.5 Private Members
Dart uses a leading underscore to denote private members (similar to Python’s convention):
class MyClass {
int _privateField; // Private to the library
int publicField; // Public
void _privateMethod() { } // Private method
void publicMethod() { } // Public method
}1.0.6 Library and Package Naming
Libraries and packages use lowercase with underscores:
library my_library; // ✅
import 'package:flutter_bloc/flutter_bloc.dart'; // ✅1.0.7 Enum Naming
Enums follow the same pattern as classes (UpperCamelCase), with values also in UpperCamelCase:
enum Status {
pending, // ✅ (lowercase preferred in Dart 2.15+)
approved,
rejected
}
// Or traditionally:
enum Status {
Pending, // Also acceptable
Approved,
Rejected
}1.0.8 Type Parameters
Single letter uppercase for simple cases, or descriptive UpperCamelCase:
class Box<T> { } // ✅ Simple generic
class Cache<Key, Value> { } // ✅ Descriptive generics
Map<String, List<int>> data; // ✅ Nested generics1.0.9 Extensions
Extensions use UpperCamelCase:
extension StringExtension on String { // ✅
bool get isEmail => contains('@');
}1.0.10 Mixins
Mixins follow class naming conventions:
mixin ValidationMixin { } // ✅
mixin validationMixin { } // ❌1.0.11 Annotations
Annotations use lowerCamelCase:
@override
void method() { }
@deprecated
void oldMethod() { }1.0.12 Import Organization
Imports should be organized in specific order:
// 1. Dart imports
import 'dart:async';
import 'dart:io';
// 2. Package imports
import 'package:flutter/material.dart';
import 'package:http/http.dart';
// 3. Local imports
import '../models/user.dart';
import 'widgets/custom_button.dart';1.0.13 Formatting Guidelines
Key formatting rules that differ from Python:
// Use 2 spaces for indentation (not 4 like Python)
class Example {
void method() {
if (condition) {
// 2 spaces per level
}
}
}
// Opening braces on same line (like JavaScript)
if (condition) { // ✅
// code
}
if (condition) // ❌
{
// code
}
// Trailing commas for better formatting
Widget build(BuildContext context) {
return Container(
padding: EdgeInsets.all(16),
child: Text('Hello'), // Trailing comma helps formatting
);
}1.0.14 Documentation Comments
Use triple-slash for documentation (similar to Python’s docstrings but different syntax):
/// Calculates the area of a circle.
///
/// Takes [radius] as input and returns the calculated area.
/// Throws [ArgumentError] if radius is negative.
double calculateArea(double radius) {
if (radius < 0) throw ArgumentError('Radius cannot be negative');
return 3.14159 * radius * radius;
}1.0.15 Common Flutter-Specific Conventions
When working with Flutter specifically:
// StatelessWidget class names
class MyHomePage extends StatelessWidget { } // ✅
// State class naming
class _MyHomePageState extends State<MyHomePage> { } // ✅
// Build method always returns Widget
@override
Widget build(BuildContext context) {
return Container();
}1.0.16 Quick Reference Comparison
Here’s how Dart compares to your familiar languages:
| Element | Dart | Python | JavaScript |
|---|---|---|---|
| Files | snake_case | snake_case | kebab-case/camelCase |
| Classes | UpperCamelCase | UpperCamelCase | UpperCamelCase |
| Functions | lowerCamelCase | snake_case | camelCase |
| Constants | lowerCamelCase | UPPER_SNAKE | UPPER_SNAKE |
| Private | _leading | _leading | #private |
Remember to use the Dart formatter (dart format) to automatically handle most spacing and line break conventions. In VS Code, you can enable format on save for Dart files to maintain consistent styling automatically.