Skip to main content

avoid-notifier-constructors

added in: 1.19.0
Pro+

Warns when a Notifier (or AsyncNotifier) has a non-empty constructor.

Having logic inside the constructor can lead to state initialization issues and unexpected behavior.

Example

❌ Bad:

class Counter extends Notifier<int> {
var state = 0;

// LINT: Avoid creating constructors for notifiers. Try moving the constructor logic to the 'build' method.
Counter() {
state = 1;
}


int build() {
return state;
}

void increment() {
state++;
}
}

✅ Good:

class Counter extends Notifier<int> {

int build() {
return 0; // Correct, initialized in the build method.
}

void increment() {
state++;
}
}