prefer_multi_bloc_provider
v0.4.0 Warning Fix Bloc / Riverpod
This rule flags nested BlocProvider, BlocListener, or RepositoryProvider widgets that could be consolidated using their Multi* counterpart. It only triggers when the same type is nested (e.g., BlocProvider inside BlocProvider), not when mixing different types.
Why use this rule
Section titled “Why use this rule”Deeply nested providers create a “pyramid of doom” that hurts readability and makes diffs harder to review. MultiBlocProvider, MultiBlocListener, and MultiRepositoryProvider flatten the nesting into a clean list, keeping the widget tree shallow and easy to scan. The behavior is identical — it’s purely a readability improvement.
See also: MultiBlocProvider | MultiBlocListener | MultiRepositoryProvider
import 'package:flutter/widgets.dart';import 'package:flutter_bloc/flutter_bloc.dart';
abstract class CounterEvent {}
class CounterBloc extends Bloc<CounterEvent, int> { CounterBloc() : super(0);}
class TimerCubit extends Cubit<int> { TimerCubit() : super(0);}
// Nested BlocProvidersfinal widget = BlocProvider<CounterBloc>( create: (context) => CounterBloc(), child: BlocProvider<TimerCubit>( create: (context) => TimerCubit(), child: Container(), ),);import 'package:flutter/widgets.dart';import 'package:flutter_bloc/flutter_bloc.dart';
abstract class CounterEvent {}
class CounterBloc extends Bloc<CounterEvent, int> { CounterBloc() : super(0);}
class TimerCubit extends Cubit<int> { TimerCubit() : super(0);}
// Flattened with MultiBlocProviderfinal widget = MultiBlocProvider( providers: [ BlocProvider<CounterBloc>(create: (context) => CounterBloc()), BlocProvider<TimerCubit>(create: (context) => TimerCubit()), ], child: Container(),);Configuration
Section titled “Configuration”This rule is in the opinionated preset, so it is on with
preset: opinionated, or by name:
rules: prefer_multi_bloc_provider: trueTo turn it off again:
rules: prefer_multi_bloc_provider: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_bloc_public_methods— Prevent public methods, getters, and setters in Bloc classes.avoid_duplicate_bloc_event_handlers— Register each bloc event type exactly once.avoid_passing_bloc_to_bloc— Prevent Bloc/Cubit classes from depending on other Bloc/Cubit instances.emit_new_bloc_state_instances— Emit a new state instance instead of the existing state object.