Skip to main content

avoid-bloc-public-methods

added in: 1.2.0
🛠
Pro+

Warns when a Bloc has public methods except the overridden ones.

When creating a cubit, it's recommended to only expose public methods for the purposes of triggering state changes. As a result, generally all public methods on a cubit instance should return void or Future<void>.

When creating a bloc, it's recommended to avoid exposing any custom public methods and instead notify the bloc of events by calling add.

Example

❌ Bad:

class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0);

// LINT: Avoid declaring public methods for 'Blocs'. Try making them private instead.
void changeSate(int newState) {
state = newState;
}


void onChange(Change<int> change) {
super.onChange(change);
print(change);
}
}

✅ Good:

class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0);


void onChange(Change<int> change) {
super.onChange(change);
print(change);
}

void _changeSate(int newState) {
state = newState;
}
}

Additional Resources