Closed cedvdb closed 3 years ago
Hi @cedvdb 👋 Thanks for opening an issue!
I would highly recommend against this because it results in tightly coupling the widget which is creating the bloc with the widget that is consuming the bloc. This makes it extremely difficult to write widget tests because they will be using the real bloc and it's not possible to provide a mock.
For an example, you can refer to the weather example and note that the role of the WeatherPage
is to provide an instance of a WeatherCubit
to the WeatherView
. Then the WeatherView
is just consuming the cubit state and has no idea where it came from.
We can then test the WeatherView very easily by providing a Mock
instance of the WeatherCubit
.
Hope that helps 👍
I see, good observation as always. Annotations would be nice (without a builder) though .
Closing
@felangel Do you see any drawbacks of doing it like this (and bubbling everything to the top):
class PreferencesScreen extends StatelessWidget {
final PreferencesBloc _preferencesBloc = PreferencesBloc();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: SentAppBar(
body: BlocBuilder<PreferencesBloc, FormxState>(
bloc: _preferencesBloc,
builder: (ctx, state) => state is FormxStateForm
? PreferencesForm(
value: state.initialValue,
onChange: (value) => _preferencesBloc.submit(value),
)
: Spinner(),
),
);
}
}
@cedvdb yes, you should avoid doing that because the bloc will never be disposed properly.
Is your feature request related to a problem? Please describe. I would like to avoid nesting unnecessarily by having a BlocProvider + a Bloc Consumer for screens cubit. This would make the use leaner of the UI.
Describe the solution you'd like instead of this
Having this:
or