In Dart, const is used to declare compile-time constants. It can also be used to declare constant values, typically provided by constant constructors. This constructor, if used within const
context will create an instance as a compile-time constant. This is an example of usage of a constant constructor:
Declaration
class Person {
final int age;
final String name;
const Person(this.age, this.name);
}
Usage
void f() {
var p = const Person(40, 'A');
var family = const [Person(40, 'A'), Person(39, 'B')];
}
When you’re already inside the const context, there’s no need to repeat the keyword. So instead of writing const [const
Person(40, 'A'), const Person(39, 'B')] you can just write const [Person(40, 'A'), Person(39, 'B')].
This rule raises an issue when const modifier was used within another const context
Noncompliant code example
void f() {
var family = const [const Person(40, 'A'), const Person(39, 'B')];
}
Compliant solution
void f() {
var family = const [Person(40, 'A'), Person(39, 'B')];
}