Flutter dart naming conventions
Identifiers come in three flavors in Dart:
- UpperCamelCase names capitalize the first letter of each word, including the first.
- lowerCamelCase names capitalize the first letter of each word, except the first which is always lowercase, even if it’s an acronym.
- lowercase_with_underscores names use only lowercase letters, even for acronyms, and separate words with _.
Name class names, types and extensions using UpperCamelCase
class SliderMenu { ... }
class HttpRequest { ... }
typedef Predicate = bool Function(T value);
class Foo {
const Foo([Object? arg]);
}
@Foo(anArg)
class A { ... }
@Foo()
class B { ... }
const foo = Foo();
@foo
class C { ... }
extension MyFancyList on List { ... }
extension SmartIterable on Iterable { ... }
Name libraries, packages, directories, source files and import prefixes using lowercase_with_underscores. Some file systems are not case-sensitive, so many projects require filenames to be all lowercase. Using a separating character allows names to still be readable in that form. Using underscores as the separator ensures that the name is still a valid Dart identifier, which may be helpful if the language later supports symbolic imports.
library peg_parser.source_scanner; import 'file_system.dart'; import 'slider_menu.dart'; import 'dart:math' as math; import 'package:angular_components/angular_components.dart' as angular_components; import 'package:js/js.dart' as js;
Name class members, top-level definitions, variables, constants, parameters, and named parameters using lowerCamelCase.
const pi = 3.14;
const defaultTimeout = 1000;
final urlScheme = RegExp('^([a-z]+):');
class Dice {
static final numberGenerator = Random();
}
class HttpConnection {}
class DBIOPort {}
class TVVcr {}
class MrRogers {}
var httpRequest = ...
var uiHandler = ...
var userId = ...
Id id;
Using _, __, etc. for unused callback parameters.
futureOfVoid.then((_) {
print('Operation complete.');
});
Reference: https://dart.dev/guides/language/effective-dart/style
Search within Codexpedia
Search the entire web