Skip to main content

avoid-passing-bloc-to-bloc

added in: 1.2.0

Warns when a Bloc depends on another Bloc.

Example

❌ Bad:

class BadBloc extends Bloc<CounterEvent, int> {
final OtherBloc otherBloc;
late final StreamSubscription otherBlocSubscription;

// LINT
BadBloc(this.otherBloc) : super(0) {
otherBlocSubscription = otherBloc.stream.listen((state) {
add(Change(1));
});
}


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

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


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

void _listenToChange() {}
}

✅ Good:

class GoodBloc extends Bloc<CounterEvent, int> {
// You can pass the stream instead
GoodBloc() : super(0) { }


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