Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Dart blank character


May 23, 2021 Dart Code style guide


Table of contents


Blank character

Like other languages, Dart ignores white space. B ut that's not the case. By adding spaces to your code to style it, you can make people see something similar to what the compiler sees.

Do not use Tab indentation.

Formatting your code with blank characters ensures that everyone sees the same thing in the editor. This can also be the same style as it is sent to a blog, or some code site, such as Github.

Today's editor simulates tab keys when indented to make it easier to write code and to ensure code consistency.

You should avoid writing code that is more than eighty characters long on a line.

Readability studies have shown that text that is too long is not suitable for reading because your eyes move too far when you see the next line. That's why the text of news and magazines is multi-line.

If you want a line of code to be more than eighty characters long and a little too compact for the reader. Do you really want to AbstractWidgetFactoryManagerBuilder

You should place the binary operator on the first line of a multi-line expression.

The parameters of the various styles are valid, but in most cases the code looks like this, and it's easier to ensure code consistency.

// good
if (isDeepFried ||
    (hasPieCrust && !vegan) ||
    containsBacon) {
  print('Bob likes it.');
}

Note that this can also be => the :

// good
bobLikes() =>
    isDeepFried || (hasPieCrust && !vegan) || containsBacon;
// bad
bobLikes()
    => isDeepFried || (hasPieCrust && !vegan) || containsBacon;

You should place the thyme operator on the next line of a multi-line expression.

Similarly, if you line before the operator, all operators will break before.

// good
return someCondition
    ? whenTrue
    : whenFalse;
// bad
return someCondition ?
    whenTrue :
    whenFalse;
return someCondition
    ? whenTrue : whenFalse;

In a multi-line expression, you should add the . operator . next line.

This rule takes precedence over the previous one. But other operators are different, and if you . to separate an expression, you should place it at the beginning of the second line.

// good
someVeryLongVariable.withAVeryLongProperty
    .aMethodOnThatObject();

Two blocks should be indented in the code block.

// good
if (condition) {
  print('hi');
}

If the code is too long and needs to be retented, the next line should be indented into four grids.

// good
someLongObject.aReallyLongMethodName(longArg, anotherLongArg,
    wrappedToNextLine);

You can also indent as you like:

// good
someLongObject.aReallyLongMethodName(longArg, anotherLongArg,
                                     wrappedToNextLine);

Note that => to use the

// good
bobLikes() =>
    isDeepFried || (hasPieCrust && !vegan) || containsBacon;
// bad 
bobLikes() =>
  isDeepFried || (hasPieCrust && !vegan) || containsBacon;

If the last line of a row is a function expression, the row should not be indented.

An exception to this rule above is that a function expression is nested in another expression, for example, as an argument passed to a method. These should be written in the following format:

// good
new Future.delayed(const Duration(seconds: 1), () {
  print('I am a callback');
});
// bad
new Future.delayed(const Duration(seconds: 1), () {
      print('I am a callback');
    });

The braces should be placed after the line before it.

// good
class Foo {
  method() {
    if (true) {
      // ...
    } else {
      // ...
    }
  }
}

For all control statements, parentheses should be used.

Doing so helps avoid the problem of else hanging.

// good
if (true) {
  print('sanity');
} else {
  print('opposite day!');
}
// bad
if (true) print('sanity');
else
  print('opposite day!');

There is a special case in this case: if statement can be else without the corresponding else statement.

// good
if (arg == null) return defaultValue;

The case statement in the switch statement should indent two spaces, and the function body of the case statement should indent four spaces.

// good
switch (fruit) {
  case 'apple':
    print('delish');
    break;

  case 'durian':
    print('stinky');
    break;
}

Do not add spaces between the name of a function, operator, or setter and its list of parameters.

// good
bool convertToBool(arg) { ... }
bool operator ==(other) { ... }
set contents(value) { ... }
// bad
bool convertToBool (arg) { ... }
bool operator == (other) { ... }
set contents (value) { ... }

After operator keyword, there should be a space.

// good
bool operator ==(other) => ...;
// bad
bool operator==(other) => ...;

The binary, binary operator should be surrounded by a space, and after the comma should also be a space, but should not be left around the binary operator.

Note < used in > it is used as a binary operator, but not as a generic. is and !is only be used as binary operators. In addition, . spaces should never be inserted when used to access members.

// good
a = 1 + 2 / (3 * -b);
c = !condition == a > b;
d = condition ? b : object.method(a, b, c);
if (obj is! SomeType) print('not SomeType');
// bad
a=1+2/(3* - b);
c= ! condition==a>b;
d= condition?b:object.method(a,b,c);
if (obj is !SomeType) print('not SomeType');

In the loop statement, in around and for each ; After that, it should all be a space.

// good
for (var i = 0; i < 100; i++) {
  // ...
}

for (final item in collection) {
  // ...
}

Stream-controlled keywords should be followed by a space.

This is different from a function call, in which there should be no space between the function name and the parenthesis.

// good 
while (foo) {
  // ...
}

try {
  // ...
} catch (e) {
  // ...
}

There ( [ { } ( , ) ]

Similarly, < leave > of a generic if and if they are used as generics.

// good
var numbers = <int>[1, 2, (3 + 4)];

There should be a space { s in the function body.

The above rules have a special case. If { s is used after a list of parameters for a ) there should be a space between .

// good
getEmptyFn(a) {
  return () {};
}
// bad
getEmptyFn(a){
  return (){};
}

Initialization in the constructor should ensure that each field occupies a separate line.

// good
MyClass()
    : firstField = "some value",
      secondField = "another",
      thirdField = "last" {
  // ...
}

Note: : should be placed at the beginning of the next line of the function name and should be indented into four grids. The fields should be aligned (that is, the first field is indented into six totals).

For named ginseng and shaped ginseng, : be a space after : .

// good
class ListBox {
  bool showScrollbars;

  ListBox({this.showScrollbars: false});
}

main() {
  new ListBox(showScrollbars: true);
}
// bad
new ListBox(showScrollbars:true);
new ListBox(showScrollbars : true);

Optional position parameters = spaced around .

// good
class HttpServer {
  static Future<HttpServer> listen([int port = 80]) {
    // ...
  }
}
// bad
import 'dart:async';

class HttpServer {
  static Future<HttpServer> listen([int port=80]) {
    // ...
  }
}

The cascading of methods should be indented into two grids.

// good
list = new List()
  ..addAll([1, 2, 3])
  ..addAll([4, 5, 6]);
// bad
list = new List()
    ..addAll([1, 2, 3])
    ..addAll([4, 5, 6]);

Previously, this rule was "indented into four grids," but based on our findings on existing code, cascading is more common with two-indentation.

If the multi-line list and the literal value of the mapped code are ] in } the form of multiple lines, you should indent the two lines and place the closed , or , on the next line.

The multi-line list and the literal value of the map are best lined after the comma.

] Or, } should indent according to the context code to match the format of the context code.

// good
var simpleList = [1, 2, 3];
var simpleMap = {'a': 1, 'b': 2, 'c': 3};

var fooList = [
  'a',
  'b',
  'c'
];

var barMap = {
  'a': 'b',
  'c': 'd'
};

var listInsideMap = {
  'a': ['b', 'c', 'd'],
  'e': [
    'f',
    'g',
    'h'
  ]
};

var mapInsideMap = {
  'a': {'b': 'c', 'd': 'e'},
  'f': {
    'f': 'g',
    'h': 'i'
  }
};

var mapInsideList = [
  {
    'a': 'b',
    'c': 'd'
  },
  {
    'a': 'b',
    'c': 'd'
  },
];
// bad
var fooList = [
    1,
    2,
    3
];

var fooList = [
  1,
  2,
  3];

var fooList = [1, 2,
  3, 4];