Skip to content

fix(flutter_bloc): extend InheritedProvider to enable _collapseProviders - #4922

Open
GhagSagar23 wants to merge 1 commit into
felangel:masterfrom
GhagSagar23:fix/bloc-provider-inherit-from-inherited-provider
Open

GhagSagar23 wants to merge 1 commit into
felangel:masterfrom
GhagSagar23:fix/bloc-provider-inherit-from-inherited-provider

Conversation

@GhagSagar23

Copy link
Copy Markdown

Summary

BlocProvider previously extended SingleChildStatelessWidget and created InheritedProvider internally in buildWithChild. This meant MultiProvider._collapseProviders — which merges adjacent InheritedProvider subclasses into a single SingleChildBuilder — skipped BlocProviders entirely (the provider is InheritedProvider check failed at the first BlocProvider, breaking the loop).

Each uncollapsed provider adds ~5-8 JS stack frames during Flutter's recursive Element.rebuild chain in DDC debug mode. With 119+ BlocProviders in a MultiBlocProvider, this exhausts Chrome's call stack on web debug (stack overflow).

By extending InheritedProvider<T> directly — the same approach RepositoryProvider uses via Provider<T> — BlocProvider now participates in _collapseProviders, matching MultiRepositoryProvider's behavior and dramatically reducing widget tree depth.

Repro results (Chrome DDC debug, same machine)

Provider count MultiBlocProvider (before) MultiRepositoryProvider (collapsed)
120 ✅ clean ✅ clean
135 ❌ stack overflow ✅ clean
200 ❌ stack overflow ✅ clean

Breaking changes

  • BlocProvider constructors are no longer const (InheritedProvider's constructors aren't const). In practice this rarely affects real code since the create parameter is almost always a closure.
  • The assertion message when child is not specified now comes from InheritedProvider ("used outside of MultiProvider" vs "used outside of MultiBlocProvider"). Tests updated to use contains matcher.

Test plan

  • All 94 existing flutter_bloc tests pass
  • bloc_provider_test.dart assertion tests updated for new message source
  • Verified repro: MultiBlocProvider with 135 BlocProviders no longer crashes on Chrome DDC debug

Closes #4893
Relates to #4781

BlocProvider previously extended SingleChildStatelessWidget and
created InheritedProvider internally in buildWithChild. This meant
MultiProvider._collapseProviders skipped BlocProviders entirely
(the `is InheritedProvider` check failed), causing each provider
to add ~5-8 JS frames to the call stack during rebuild.

With 119+ BlocProviders in a MultiBlocProvider, this exhausts
Chrome's call stack in DDC debug mode (stack overflow on web).

By extending InheritedProvider directly, BlocProvider now
participates in _collapseProviders, matching RepositoryProvider's
behavior and dramatically reducing widget tree depth.

Closes felangel#4893
Relates to felangel#4781

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Issue: Flutter Web Debug Stack Overflow with Large MultiBlocProvider Configuration

1 participant