Introduction to the

Each language has its own code style, which is closely related to language features. If we follow the uniform coding rules in the process of coding, it will bring much convenience to our business.

Dart also has its own coding style. Take a look.

Naming rules

In general, there are three kinds of naming rules in the world, were UpperCamelCase, lowerCamelCase and lowercase_with_underscores.

UpperCamelCase is the camel case, so the first letter is uppercase and the other letters are lowercase.

LowerCamelCase is also in camel case, except that the first letter of the word is lowercase.

Lowercase_with_underscores is words that are joined with underscores.

For classes, typedefs, and enumerations of these types, the UpperCamelCase mode is generally used:

class ClassRoom {}

typedef Predicate<T> = bool Function(T value);
Copy the code

For instances of the class, use lowerCamelCase:

const classRoom = ClassRoom();
Copy the code

For method names, use lowerCamelCase as well:

void main() {
}
Copy the code

Previously we talked about the extension introduced in DART 2.7, which also requires the UpperCamelCase:

extension StringCompare on String { ... }
Copy the code

Lowercase_with_underscores for libraries, packages, directories, and source files, as shown below:

library common_convert.string_convert;

import 'lib_one.dart';
import 'lib_two.dart';
Copy the code

Lowercase_with_underscores if you want to rename the import lib, use lowercase_with_underscores, as follows:

import 'lib_one.dart' as lib_one;
Copy the code

For some callback arguments that are not used, we can use _ instead:

futureOfVoid.then((_) {
  print('Operation complete.');
});
Copy the code

If it is a private property, 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, and generally we don’t pay much attention to the order of import during coding.

Dart does, however, recommend the order of the imports.

First “dart:” needs to precede all other imports:

import 'dart:html';

import 'package:bar/bar.dart';
Copy the code

And “package:” needs to precede the internal project reference:

import 'package:foo/foo.dart';

import 'util.dart';
Copy the code

If export is required, export needs to be separated from import:

import 'src/foo_bar.dart';

export 'src/error.dart';
Copy the code

The specific imports are then sorted alphabetically in the order mentioned above.

formatting

For DART, the DART language itself does not recognize whitespace, but for humans, the code needs to be formatted with whitespace to make it readable.

Dart provides the dart format command for uniform formatting.

While the Dart Format command does 99% of the work for you, you still need to do 1% of the work yourself.

For example: a line of no more than 80 characters, all flow control statements are enclosed in braces, and so on.

conclusion

That’s a summary of the code style in DART.

This article is available at www.flydean.com/27-dart-sty…

The most popular interpretation, the most profound dry goods, the most concise tutorial, many tips you didn’t know waiting for you to discover!

Welcome to pay attention to my public number: “procedures those things”, understand technology, more understand you!