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

Dart name


May 23, 2021 Dart Code style guide


Table of contents


Name

When naming, it should take the form of a hump case.

Classes, enumerations, and custom types should capitalization of the first letter of each word, including the first word, and should not use separators.

// good
class SliderMenu {
  // ...
}

class HttpRequest {
  // ...
}

typedef num Adder(num x, num y);

It is best to name the constant in the form of lowerCamelCase

Constants are lowerCamelCase and values in enumeration types should also be in this form.

If your existing code is named for a constant in full capital, you can open the capital lock to keep your code consistent.

// good
const pi = 3.14;
const defaultTimeout = 1000;
final urlScheme = new RegExp('^([a-z]+):');

class Dice {
  static final numberGenerator = new Random();
}
// bad
const PI = 3.14;
const kDefaultTimeout = 1000;
final URL_SCHEME = new RegExp('^([a-z]+):');

class Dice {
  static final NUMBER_GENERATOR = new Random();
}

Change: In previous code we recommended that constant naming use full capital. S ince const is now separated from final, we changed the naming recommendation. Of course, there are those who would like to enumeration in full capitals, but we would like to take into question the current state of the Guide.

For other identifiers, you should use lowerCamelCase

Class members, global definitions, variables, parameters, and named parameters should capitalt all words other than the first word, and the separator should not be used.

//good
var item;

HttpRequest httpRequest;

align(clearItems) {
  // ...
}

For classes used in metadata annotations, they should be UpperCamelCase

If the annotation does not require any parameters, you may want to create lowerCamelCase for it.

// good 
@Foo(anArg)
class A { ... }

@Foo()
class B { ... }

@foo
class C { ... }

For libraries and source files, you should lowercase_with_underscores the file.

Some file systems are case insensoic, so many projects require file names to be all in small case. I n this case, use underscores to separate the words in the file name so that the file name remains readable. The name that uses underscores as separators is still a valid Dart identifier, a feature that would be useful if Dart later added support for symbolic references.

Good example:

  • slider_menu.dart
  • file_system.dart
  • library peg_parser

Bad example:

  • SliderMenu.dart
  • filesystem.dart
  • library peg-parser

When declaring the prefix of a lowercase_with_underscores the library.

// good
import 'dart:math' as math;
import 'dart:json' as json;
import 'package:js/js.dart' as js;
import 'package:javascript_utils/javascript_utils.dart' as js_utils;
// bad
import 'dart:math' as Math;
import 'dart:json' as JSON;
import 'package:js/js.dart' as JS;
import 'package:javascript_utils/javascript_utils.dart' as jsUtils;

The preceding library name should be preceded by the package name . the paths with .

This guide will help you avoid warnings that are caused by two libraries having the same name. Here are some of the rules we recommend:

  • All library names are prefixed with the package name.
  • The name of the entry library should be the same as the package name.
  • For other libraries in the package, use . after the package . the paths of the Dart files that correspond to the library. If lib a library under lib, you don't have to add lib to your name.

For example, if the package name my_package Here's the name of the other library files in the package:

// good 
// In lib/my_package.dart
library my_package;

// In lib/other.dart
library my_package.other;

// In lib/foo/bar.dart
library my_package.foo.bar;

// In example/foo/bar.dart
library my_package.example.foo.bar;

// In lib/src/private.dart
library my_package.src.private;

For acronyms and abbreviations with more than two letters, it should be capitaled.

Capital acronyms can be a bit difficult to read, and can cause ambiguity when the word consists of multiple adjacent words. With HTTPSFTPConnection you can't tell if it's an HTTPS FTP connection or an HTTP SFTP connection.

To avoid this, acronyms and abbreviations are written like regular words and can be named according to your personal preference if you have only two letters. (Two-letter abbreviations, like ID and Mr. can also be written like regular words)

// good
HttpConnection
uiHandler
IOStream
HttpRequest
Id
id
Pt
DB
// bad
HTTPConnection
UiHandler
IoStream
HTTPRequest
ID
PT
Db