dart-lang / dart-lang/language
Make .runtimeType aware of necessary superclasses
- Dominant language
- TeX
- Stars
- 2.9k
- Forks
- 239
- Avg merge
- 2d 18h
- Merged PRs (30d)
- 14
Description
The [bloc library ](https://bloclibrary.dev/#/gettingstarted)is very useful for structuring larger projects.
In one [example ](https://github.com/felangel/bloc/blob/master/examples/flutter_weather/lib/main.dart) it illustrates how to declare multiple providers:
```
MultiBlocProvider(
providers: [
BlocProvider(
builder: (context) => ThemeBloc(),
),
BlocProvider(
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 blocs
MultiBlocProvider(
Key key,
this.child,
this.blocs,
)
BlocProvider _createProvider(Bloc bloc){
return BlocProvider(
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.
Contributor guide
Assessment
This issue has not been assessed yet.