The naming table
| Identifier | Convention | Example |
|---|---|---|
| Classes, enums, typedefs, type parameters, extensions | UpperCamelCase | SliderMenu |
| Packages, directories, source files | lowercase_with_underscores | file_system.dart |
| Import prefixes | lowercase_with_underscores | as math |
| Members, variables, parameters, top-level functions | lowerCamelCase | clearItems() |
| Constants and enum values | lowerCamelCase | defaultTimeout |
| Private members | leading underscore | _cache |
Constants are lowerCamelCase
Effective Dart says to prefer lowerCamelCase for constants in new code, enum values included: const defaultTimeout = 1000;. SCREAMING_CAPS is acceptable only to match existing code, or code generated alongside Java such as protobuf enums.
Acronyms: the two-letter rule
An acronym or abbreviation longer than two letters is capitalized like a word: HttpRequest, Uri, and HttpsFtp rather than HTTPSFTP. A two-letter one keeps both capitals if both are capitals in English, as with ID, TV and UI. At the start of a lowerCamelCase name it's all lowercase: httpConnection.
Underscores mean private
In Dart a leading underscore isn't a convention, it's the language's privacy marker: it makes a member or top-level declaration private to its library. Effective Dart says not to put one on local variables, parameters or local functions, since they're already private.
No prefix letters
Dart style drops Hungarian-style prefixes such as kDefaultTimeout. Write defaultTimeout; the tools already show a name's type, scope and mutability.
Converting names with Transform Case
Our snake_case, camelCase, PascalCase and CONSTANT_CASE converters treat an acronym as one word, so HTTP server error becomes HttpServerError and http_server_error. For acronyms of three letters or more that matches Effective Dart. For two-letter acronyms that are capitals in English, put them back: userId becomes userID.
Naming conventions in other languages: Python, Java, C#, Go (Golang), Rust, Kotlin, Swift, PHP. All side by side: 20 languages compared.
Sources
- Effective Dart: Style, Dart (dart.dev)
Questions
Should Dart constants be ALL_CAPS?
No. Effective Dart prefers lowerCamelCase for constants in new code, such as defaultTimeout.
How should Dart files be named?
lowercase_with_underscores, such as file_system.dart. Packages and directories use the same style.
Is it HTTPRequest or HttpRequest in Dart?HttpRequest. Acronyms longer than two letters are capitalized like words.