Introduction
Each language has its own coding style, which is closely related to language features. If we follow this unified coding rule in the coding process, it will bring a lot of convenience to our business.
Similarly, corresponding to dart only, it also has its own coding style, let's take a look.
naming convention
Generally speaking, there are three naming rules in this world, UpperCamelCase, lowerCamelCase and lowercase_with_underscores.
UpperCamelCase represents the camel case format, that is, the first letter is capitalized, and the other letters are lowercase.
The lowerCamelCase is also in camel case, the difference is that the first letter of its first word is lowercase.
lowercase_with_underscores connects words with underscores.
For classes, typedefs, and enumerations, the UpperCamelCase mode is generally used:
class ClassRoom {}
typedef Predicate<T> = bool Function(T value);
For instances of classes, use lowerCamelCase:
const classRoom = ClassRoom();
For method names, also use lowerCamelCase:
void main() {
}
Earlier we talked about the extension introduced by dart 2.7, and the extension also needs to use UpperCamelCase:
extension StringCompare on String { ... }
For libraries, packages, directories and source files, use lowercase_with_underscores as follows:
library common_convert.string_convert;
import 'lib_one.dart';
import 'lib_two.dart';
If you want to rename the imported lib, you need to use lowercase_with_underscores as follows:
import 'lib_one.dart' as lib_one;
For the parameters in some callback functions, if they are not used, you can use _ instead:
futureOfVoid.then((_) {
print('Operation complete.');
});
If it is a private attribute, it is recommended to prefix the name with _ to indicate that it is a private value.
order in import
In dart, we need to use other packages. Generally speaking, we do not pay special attention to the order of imports during the coding process.
However, dart also recommends the order of imports.
First "dart:", which needs to be placed before all other imports:
import 'dart:html';
import 'package:bar/bar.dart';
And "package:" needs to be placed before the internal project reference:
import 'package:foo/foo.dart';
import 'util.dart';
If you need to export, export needs to be distinguished from import:
import 'src/foo_bar.dart';
export 'src/error.dart';
The specific imports are then sorted alphabetically in the order mentioned above.
format
For dart, the dart language itself does not recognize spaces, but for humans, it is necessary to format the code with spaces so that it can be read well.
To unify the format, dart provides the dart format command.
While the dart format command does 99% of the work for you, there is another 1% that you need to do yourself.
For example: a line does not exceed 80 characters, all flow control statements are enclosed in curly brackets, etc. Some other work that should be paid attention to.
Summarize
The above is a summary of the code style in dart.
This article has been included in http://www.flydean.com/27-dart-style/
The most popular interpretation, the most profound dry goods, the most concise tutorials, and many tricks you don't know are waiting for you to discover!
Welcome to pay attention to my official account: "Program those things", understand technology, understand you better!
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。