Skip to main content

avoid-empty-build-when

added in: 1.23.0
Free+

Warns when a BlocBuilder or BlocConsumer does not specify the buildWhen condition.

Specifying buildWhen helps avoid unnecessary rebuilds and improves overall performance.

Example

❌ Bad:

// LINT: Avoid empty 'buildWhen'. Try specifying it to prevent unnecessary rebuilds.
BlocConsumer<BlocA, BlocAState>(
builder: (context, state) {
...
},
);

// LINT: Avoid empty 'buildWhen'. Try specifying it to prevent unnecessary rebuilds.
BlocBuilder<BlocA, BlocAState>(
builder: (context, state) {
...
},
);

✅ Good:

BlocConsumer<BlocA, BlocAState>(
buildWhen: (previous, current) {
...
},
builder: (context, state) {
...
},
);

BlocBuilder<BlocA, BlocAState>(
buildWhen: (previousState, state) {
...
},
builder: (context, state) {
...
},
);