Skip to content

Issue: Flutter Web Debug Stack Overflow with Large MultiBlocProvider Configuration #4893

Open

Description

@balapraveen-rm

Flutter Web Debug Stack Overflow with Large MultiBlocProvider Configuration

Summary

We are experiencing a stack overflow / call-stack exhaustion issue in Flutter Web debug mode when running our application with a large number of globally registered BlocProvider / Cubit instances through MultiBlocProvider.

The application works as expected on mobile platforms, but Flutter Web debug mode can fail during application startup/navigation with a stack overflow in the browser renderer.

We would like to understand whether this behavior is related to the interaction between the BLoC package's MultiBlocProvider implementation and the Flutter Web debug runtime.

Environment

Flutter

Flutter 3.47.2 • channel stable
Framework • revision d3b14c8769 (2026-08-26)
Engine • revision a804b26164
Dart 3.13.2
DevTools 2.60.0

Application

Platform: Flutter Web
Build mode: Debug
Browser: Google Chrome
State management: flutter_bloc

Application State Manager Audit

  • 107 BLoCs
  • 12 Cubits
  • 119 total state managers

Problem Description

The application contains a large root-level MultiBlocProvider configuration.

Conceptually, the application has a structure similar to:

MultiBlocProvider(
  providers: [
    BlocProvider<BlocA>(...),
    BlocProvider<BlocB>(...),
    BlocProvider<BlocC>(...),
    // ...
    // 119 globally registered providers
  ],
  child: MyApp(),
)

When running the application in Flutter Web debug mode, we observe a stack overflow/call-stack exhaustion failure.

The failure is particularly noticeable when entering certain screens, including the Login flow.

The affected screens themselves do not appear to contain an unusually recursive widget implementation. The issue appears to be related to the depth of the provider/widget hierarchy created by the large global MultiBlocProvider configuration.

Observed Behavior

Flutter Web Debug

Application starts
        ↓
Large MultiBlocProvider hierarchy
        ↓
Navigate/open affected screen
        ↓
Deep call stack
        ↓
Stack overflow / renderer failure

Mobile

The same application architecture does not reproduce the issue in the same way on Android/iOS.

Web Release

The issue also appears to be specific to the Web debug/runtime environment rather than being a general BLoC functionality failure.

Investigation Performed

We investigated the application architecture and performed a BLoC/Cubit usage audit.

We also temporarily reduced the number of globally registered providers and removed several unused/dead BLoCs.

After reducing the global provider count, the Web debug application became stable.

This indicates that the depth/size of the provider hierarchy is likely a significant contributing factor.

However, we do not consider the reduced provider count to be a definitive threshold or permanent solution.

Related Issue

We found a closely related BLoC issue:

#4781

The issue reports a similar Flutter Web stack overflow scenario with a large number of globally initialized BlocProviders. The reported application had 250+ providers, and reducing the provider count resolved the problem.

Our application currently has 119 providers registered through MultiBlocProvider, so the behavior appears potentially related to the same class of provider-tree/call-stack issue.

We understand that this does not establish a fixed provider-count limit, and we are not assuming that the two issues have exactly the same root cause.

Expected Behavior

A large MultiBlocProvider configuration should ideally not result in an unrecoverable stack overflow in Flutter Web debug mode.

If there is a practical limitation related to provider-tree depth, we would appreciate clarification on:

  1. Whether there is a known limitation with deeply nested MultiBlocProvider configurations on Flutter Web.
  2. Whether MultiBlocProvider introduces additional widget/element nesting for every registered provider.
  3. Whether there is a recommended architectural approach for applications with a large number of BLoCs/Cubits.
  4. Whether the current behavior is expected to be handled by flutter_bloc, Flutter Web, or another underlying dependency.
  5. Whether there are any recommended optimizations to avoid excessive call-stack depth.
  6. Whether this behavior is known to be affected by Flutter Web debug compilation/runtime behavior.

Requested Investigation

Could the maintainers please investigate whether a large number of providers in MultiBlocProvider can cause excessive call-stack depth in Flutter Web debug mode?

If possible, we would also appreciate guidance on whether this should be addressed within flutter_bloc or whether the limitation originates from the Flutter Web/Dart debug runtime.

Potential Workaround

Our current workaround is to avoid registering all feature-specific BLoCs globally.

We are considering moving feature-specific BLoCs/Cubits to route/page-level scopes, for example:

MyApp
 └── Global MultiBlocProvider
      ├── AuthenticationBloc
      ├── AppConfigurationBloc
      └── Other application-wide state
           
Routes
 ├── Dashboard
 │    └── DashboardBloc
 │
 ├── Device
 │    ├── DeviceBloc
 │    └── DeviceSettingsBloc
 │
 └── Reports
      ├── ReportsBloc
      └── ReportFilterCubit

This reduces the number of globally nested providers while keeping feature-specific state scoped to the feature that actually requires it.

References

Activity

  1. changed the title [-]# Flutter Web Debug Stack Overflow with Large MultiBlocProvider Configuration[/-] [+]Issue: Flutter Web Debug Stack Overflow with Large MultiBlocProvider Configuration[/+] on Sep 30, 2026
  2. GhagSagar23 commented on Nov 19, 2026

    @GhagSagar23

    Root Cause Analysis

    I investigated this and the root cause is in how BlocProvider interacts with MultiProvider._collapseProviders.

    The collapse gap:

    MultiProvider (from the provider package) has an optimization called _collapseProviders that merges adjacent InheritedProvider subclasses into a single SingleChildBuilder, dramatically reducing widget tree depth. However, BlocProvider extends SingleChildStatelessWidget, not InheritedProvider — so this check fails immediately:

    // provider package — MultiProvider._collapseProviders
    for (; i < providers.length; i++) {
      final provider = providers[i];
      if (provider is InheritedProvider) {  // BlocProvider fails this ❌
        // ... merge into collapsed chain
      } else {
        break;  // breaks on first BlocProvider, zero collapsing
      }
    }

    Compare: RepositoryProvider extends Provider<T> → InheritedProvider<T>, so MultiRepositoryProvider benefits from the collapse. BlocProvider doesn't because it wraps InheritedProvider internally via buildWithChild rather than extending it.

    Why web-debug only:

    DDC (Dart Dev Compiler) compiles each Dart method to a separate JS function with full debug info, assertions, and no inlining. Each widget layer in the Nested tree triggers Flutter's recursive Element.rebuild() → performRebuild() → build() → inflateWidget() → mount() chain: ~5-8 JS frames per layer. With 119+ uncollapsed BlocProviders, that's ~600-950 extra stack frames before the app's own widget tree starts — enough to exhaust Chrome's ~984 KB call stack. Release builds (dart2js) survive because aggressive inlining collapses many frames into one.

    Minimal repro:

    I reproduced this locally with a minimal project — two entry points, same provider count:

    Count MultiBlocProvider MultiRepositoryProvider
    120 ✅ clean ✅ clean
    135 ❌ crashes ✅ clean
    200 ❌ crashes ✅ clean
    // lib/main.dart — crashes at ~135 on Chrome DDC debug
    const int providerCount = 150;
    
    class _CounterCubit extends Cubit<int> {
      _CounterCubit() : super(0);
    }
    
    void main() => runApp(const App());
    
    class App extends StatelessWidget {
      const App({super.key});
    
      @override
      Widget build(BuildContext context) {
        return MultiBlocProvider(
          providers: List<BlocProvider>.generate(
            providerCount,
            (_) => BlocProvider<_CounterCubit>(create: (_) => _CounterCubit()),
          ),
          child: MaterialApp(
            home: Scaffold(
              body: Center(child: Text('$providerCount providers mounted')),
            ),
          ),
        );
      }
    }
    // lib/main_repo_provider.dart — survives 200+ on same machine
    // Same structure but with MultiRepositoryProvider + RepositoryProvider
    // (extends InheritedProvider, benefits from _collapseProviders)

    Fix:

    Opened #4922 — makes BlocProvider extend InheritedProvider<T> directly (like RepositoryProvider extends Provider<T>), so _collapseProviders can merge them. All 94 existing tests pass.

    This also relates to #4781 (same class of issue, higher provider count).

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions