-
Notifications
You must be signed in to change notification settings - Fork 222
Open
Labels
featureProposed language feature that solves one or more problemsProposed language feature that solves one or more problems
Description
The bloc library is very useful for structuring larger projects.
In one example it illustrates how to declare multiple providers:
MultiBlocProvider(
providers: [
BlocProvider<ThemeBloc>(
builder: (context) => ThemeBloc(),
),
BlocProvider<SettingsBloc>(
builder: (context) => SettingsBloc(),
),
],
child: App(weatherRepository: weatherRepository),
),
It would be much nicer if I could write:
MultiBlocProvider(
blocs: [
ThemeBloc(),
SettingsBloc(),
],
child: App(weatherRepository: weatherRepository),
),
Unfortunately, it seems currently impossible to write the MultiBlocProvider that takes the more concise input.
The straightforward way to write the concise MultiBlocProvider would be:
class MultiBlocProvider extends StatelessWidget{
final Widget child;
final List<Bloc> blocs
MultiBlocProvider(
Key key,
this.child,
this.blocs,
)
BlocProvider _createProvider(Bloc bloc){
return BlocProvider<bloc.runtimeType >(
builder: (context) => bloc,
);
}
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: blocs.map((bloc)=>_createProvider(bloc)),
child: child,
);
}
}
Currently, this doesn't work because Dart doesn't know that bloc.runtimeType
happens to be a Bloc
. Given that bloc
is declared in the code as a Bloc
it should be able for Dart to figure out that bloc
's class is a subclass of Bloc
and make the above code work.
felangel
Metadata
Metadata
Assignees
Labels
featureProposed language feature that solves one or more problemsProposed language feature that solves one or more problems