Skip to content

Make .runtimeType aware of necessary superclasses #690

@ChristianKleineidam

Description

@ChristianKleineidam

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions