Skip to main content

Dart Constant

Dart can define constant within and outside class using const keyword with lowerCamelCase name.

Example

Define constant outside class or method.

const a = 1;

void main() {
print(a);
}

Define constant within class.

class Worker {
static const int defaultThreads = 10;
}

void main() {
print(Worker.defaultThreads);
}

Naming

Use lowerCamelCase unless existing code are using ALL_CAPS_WITH_UNDERSCORE per constant_identifier_names linter rule.

References