avoid-duplicate-initializers
- Group
- Correctness
- Path
lint/correctness/avoid-duplicate-initializers- Default
- Off, opt in
- Origin
- Falcon
- Rule
- 50 of 512
Flags final/const variables with identical initializers in the same scope. Calls, construction, await, mutation, collection allocation, and closures are excluded. Non-declaration statements invalidate local candidates, so writes or calls between reads cannot turn an intentional state snapshot into a lint. Lazy static initializers are compared only when they contain no state reads.
Invalid
void fields(dynamic param) {
final first = param.value.isNotEmpty;
final second = param.value.isNotEmpty;
}
void identifiers(int input) {
final first = input;
final second = input;
}
void operators(int input) {
final first = input + 1;
final second = input + 1;
}
void constants() {
const first = 'same';
const second = 'same';
}
class A {
final first = 42;
final second = 42;
}
class B {
static const first = true;
static const second = true;
}
class LazyConstants {
static final first = 1 + 2;
static final second = 1 + 2;
}
class InstanceSnapshots {
final first = current;
final second = current;
}
int current = 0;
class MixedSnapshots {
final first = 1;
static const firstConst = 2;
static final snapshot = current;
final second = 1;
static const secondConst = 2;
} final first = param.value.isNotEmpty; final second = param.value.isNotEmpty; final first = input; final second = input; final first = input + 1; final second = input + 1; const first = 'same'; const second = 'same'; final first = 42; final second = 42; static const first = true; static const second = true; static final first = 1 + 2; static final second = 1 + 2; final first = current; final second = current; static final snapshot = current; final second = 1; final second = 1; static const secondConst = 2;Valid
void changed(int input) {
final first = input;
input = 2;
final second = input;
}
void effects(dynamic object) {
final first = object.value;
object.update();
final second = object.value;
}
void calls(dynamic object) {
final first = object.read();
final second = object.read();
}
void allocations() {
final first = Object();
final second = Object();
final third = <int>[];
final fourth = <int>[];
}
void scopes(int input) {
if (input > 0) { final first = input; }
if (input < 0) { final second = input; }
}
void distinct(dynamic object) {
final first = object.first;
final second = object.second;
final text = 'a b';
final other = 'ab';
}
void mutable(int input) {
var first = input;
var second = input;
}
class A {
final first = Object();
final second = Object();
}
class B {
final first = 1;
static final second = 1;
}
int current = 0;
final firstSnapshot = current;
final secondSnapshot = current;
class LazySnapshots {
static final first = current;
static final second = current;
static final incremented = current + 1;
static final incrementedAgain = current + 1;
static final text = 'value: $current';
static final textAgain = 'value: $current';
}Configure avoid-duplicate-initializers
Set its severity in falcon.json. See the configuration guide.
{
"linter": {
"rules": {
"correctness": {
"avoid-duplicate-initializers": "error"
}
}
}
}Or silence one occurrence with a suppression comment; the reason is required.
// falcon-ignore lint/correctness/avoid-duplicate-initializers: <reason>More correctness rules
- avoid-duplicate-map-keysrecommended
Flags repeated literal keys in map literals, including literal spreads and collection
ifentries. - avoid-duplicate-mixins
Flags repeated mixins in a with clause or a same-library superclass chain.
- avoid-duplicate-named-imports
Flags imports of the same library that differ only in their name prefix.
- avoid-duplicate-switch-case-conditionsrecommended
Flags repeated
whenguards on syntactically identical switch patterns. - avoid-duplicate-test-assertionsrecommendedtest
Reports repeated identical, side-effect-free assertions in a straight-line block.
- avoid-dynamic-calls
avoid_dynamic_callsrecommendedAvoid method calls or property accesses on a
dynamictarget. - avoid-emit-outside-blocrecommendedbloc
Flags external calls and tear-offs of a Bloc or Cubit's
emitmethod. - avoid-empty-setstaterecommendedflutter
Avoid setState callbacks with empty block bodies.