Skip to content

Conversation

@zbarbuto
Copy link

@zbarbuto zbarbuto commented Nov 11, 2025

Status

READY

Breaking Changes

NO

Maybe? If you use all lint rules this might break if you have all lint rules enable for your projevt?

Description

Adds support for a new avoid_bloc_to_bloc_members lint rule (I'm not married to the name, open to suggestions).

This was mentioned in #4264 as "Avoid passing a bloc or cubit to the constructor of another bloc or cubit" (see this comment)

The goal being to avoid tightly coupling blocs or cubits together by allowing one to be a member or parameter of another.

❌ Bad

class TightlyCoupledBloc extends Bloc {
  late final StreamSubscription otherBlocSubscription;

  TightlyCoupledBloc(OtherBloc otherBloc) {
  //                 ^^^^^^^^^^^^^^^^^^^
    otherBlocSubscription = otherBloc.stream.listen((state) {
      add(MyEvent());
    });
  }

  @override
  Future<void> close() {
    otherBlocSubscription.cancel();
    return super.close();
  }
}

❌ Bad

class FooCubit extends Cubit {
   final BarCubit otherCubit;
   //    ^^^^^^^^^^^^^^^^^^^
   FooCubit({ required this.otherCubit }): super();
}

Currently this covers both in one rule (as class fields and as parameters) but happy to split into two separate rules if that's preferable - I just figured they both fall under the same umbrella of "avoid tightly coupling blocs or cubits together".

It's done via name rather than looking at type hierarchy as that's what the other rules were doing (e.g. avoid_public_bloc_methods) - I'm not very familiar with the analyzer/diagnostic tools so unsure if there's a way to do proper checking on the parameter typings to see if a parameter actually extends Bloc or Cubit?

Also happy to extend test coverage - I don't have every single case for both bloc and cubit and every combination between but tried to cover at least one of every variation (optional vs required, named vs positional vs default, bloc vs cubit).

Type of Change

  • ✨ New feature (non-breaking change which adds functionality)
  • 🛠️ Bug fix (non-breaking change which fixes an issue)
  • ❌ Breaking change (fix or feature that would cause existing functionality to change)
  • 🧹 Code refactor
  • ✅ Build configuration change
  • 📝 Documentation
  • 🗑️ Chore

@zbarbuto zbarbuto requested a review from felangel as a code owner November 11, 2025 00:08
@zbarbuto zbarbuto force-pushed the master branch 3 times, most recently from 3b8ad69 to bb8df0d Compare November 12, 2025 22:27
@felangel
Copy link
Owner

Thanks I'll take a closer look shortly!

@felangel felangel added enhancement New feature or request pkg:bloc_lint This issue is related to the bloc_lint package labels Nov 13, 2025
@codecov
Copy link

codecov bot commented Nov 13, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@zbarbuto
Copy link
Author

Thanks - have fixed the formatting issue.
Other CI failure looks like I've missed docs somewhere, but I'm not sure?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request pkg:bloc_lint This issue is related to the bloc_lint package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants