UpperCamelCase
Style named type
Classes, enums, Typedefs, and Type Parameters, annotated with arguments, should be capitalized (including the first word) without delimiters.
class SliderMenu {... }enum Colors {yellow,red}
typedef Predicate<T> = bool Function(T value);
///The first letter of an argument is uppercase, and the first letter of an argument is lowercase
class Foo {
const Foo([Object? arg]);
}
@Foo(anArg)
class A {... }Copy the code
As with type names, the name of the Extensions should be capitalized with the first letter of each word (including the first word) and without delimiters.
extension MyFancyList<T> on List<T> { ... }
Copy the code
lowercase_with_underscores
Style named type
Some file systems are case insensitive, so many projects require that the file name be lowercase. The use of the delimiter form ensures readable naming. Using an underscore as a separator ensures that names are still valid Dart identifiers, which can be a big help if the language later supports symbol imports.
library peg_parser.source_scanner;
Copy the code
import 'dart:math' as math;
import 'package:angular_components/angular_components'
as angular_components;
import 'package:js/js.dart' as js;
Copy the code
1. Place the DART: import statement before other import statements. 2. Place the “Package:” import statement before the project-related import statement. 3. Place the export statement after all the import statements as a separate section. Dart :library > import’dart:’ > import’ package:’ > import’ util.dart’ > export ‘SRC /error.dart’ 4 Sort the statements in each section alphabetically.
import 'package:bar/bar.dart';
import 'package:foo/foo.dart';
import 'foo.dart';
import 'foo/foo.dart';
Copy the code
lowerCamelCase
Style to name other identifiers
Class members, top-level definitions, variables, parameters, and named parameters should be capitalized and undelimited except for the first word.
var count = 3;
HttpRequest httpRequest;
void align(bool clearItems) {
// ...
}
Copy the code
In the new code, use lowerCamelCase to name constants, including enumerated values.
const pi = 3.14;
const defaultTimeout = 1000;
final urlScheme = RegExp('^([a-z]+):');
class Dice {
static final numberGenerator = Random();
}
Copy the code
Treat acronyms and abbreviations longer than two letters as normal words.
Exceptions In the case of two letters, abbreviations such as IO (input/output) should be capitalized. In addition, two-letter abbreviations like “identification” are capitalized like other regular words: ID
class HttpConnection {}
class DBIOPort {}
class TVVcr {}
class MrRogers {}
var httpRequest = ...
var uiHandler = ...
Id id;
Copy the code
For unused callback arguments, it is best to use _, _, etc.
Sometimes the type signature of a callback function requires a parameter, but the callback implementation does not use this parameter. In this case, it is customary to name the unused argument _. If a function has multiple unused arguments, use additional underscores to avoid name conflicts :_, _ _, and so on.
futureOfVoid.then((_,__,___) {
print('Operation complete.');
});
Copy the code
Don’tUse prefix letters
Hungarian nomenclature and other schemes show up in BCPL when the compiler can’t help you understand your code, but because Dart can prompt you for declared types, ranges, variability, and other attributes, there’s no reason to code those attributes in the identifier name.
defaultTimeout // X kDdeaultTimeout
Copy the code
formatting
Dart, like most other languages, ignores whitespace. But people don’t. Having a consistent whitespace style helps us understand code the same way the compiler does.
Formatting is a tedious task, especially during refactoring. Fortunately, you don’t have to worry. We provide an excellent automated code formatter called Dart Format that does the formatting for you. We have some documentation on the rules that apply, and any official whitespace handling rules in Dart are generated by Dart Format.
Use curly braces for all flow control structures. So you can avoid dangling else problems.