Is your feature request related to a problem? Please describe.
For testing purposes, I need to be able to pass BlocProviders to a function something like this:
Widget baseApp({
@required Widget home,
List<SingleChildWidget> providers = const [],
...
}) {
...
return MultiProvider(
providers: [
{default providers}
...providers, // Passing providers needed to render the page being tested
],
child: MaterialApp(
home: home,
),
);
}
As you can see, I'm able to set default providers in this util rendering function, so I do not have to declare the same ceremony for each widget test. It would be great to do the same for the bloc providers, but as BlocProviderSingleChildWidget is hidden to the user of the library, I can not correctly define the parameter, otherwise, I could set the default blocs in baseApp and pass the specifics to the widget being tested, to avoid some ceremony around the widget tests.
As it is now, I need to wrap baseApp in a MultiBlocProvider and go from there, instead of passing an array of the specific blocs.
Describe the solution you'd like
Do not hide BlocProviderSingleChildWidget from the library.
Is there any problem with not hiding this type definition?
Hi @frederickcxa 馃憢
Thanks for opening an issue!
I think you should be able to achieve without needing to expose BlocProviderSingleChildWidget.
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:provider/provider.dart';
import 'package:provider/single_child_widget.dart';
void main() {
runApp(
baseApp(
home: Builder(
builder: (context) {
return Scaffold(
body: Center(
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(Provider.of<int>(context).toString()),
Text(Provider.of<String>(context)),
Text(context.bloc<CounterBloc>().state.toString()),
],
),
),
);
},
),
providers: [
BlocProvider(create: (_) => CounterBloc()),
],
),
);
}
Widget baseApp({
@required Widget home,
List<SingleChildWidget> providers = const [],
}) {
return MultiProvider(
providers: [
Provider(create: (_) => 10),
Provider(create: (_) => 'Hello World'),
...providers, // Passing providers needed to render the page being tested
],
child: MaterialApp(
home: home,
),
);
}
enum CounterEvent { increment }
class CounterBloc extends Bloc<CounterEvent, int> {
@override
int get initialState => 0;
@override
Stream<int> mapEventToState(event) async* {
switch (event) {
case CounterEvent.increment:
yield state + 1;
break;
}
}
}
Let me know if that helps 馃憤
Yeah, it worked, I thought they used diff classes, thanks!
Most helpful comment
Yeah, it worked, I thought they used diff classes, thanks!