Issue
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<TripDetailBloc>(create: (BuildContext context) => TripDetailBloc()),
BlocProvider<PopUpBloc>(create: (BuildContext context) => PopUpBloc()),
],
child: Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
BlocProvider.of<TripDetailBloc>(context).add(AddTripDetailPannelEvent());
},
),
appBar: appbar(),
body: pannel(),
)
);
}
The following assertion was thrown while handling a gesture:
- BlocProvider.of() called with a context that does not contain a Bloc of type
TripDetailBloc. - No ancestor could be found starting from the context that was passed to
BlocProvider.of<TripDetailBloc>()
. - This can happen if the context you used comes from a widget above the
BlocProvider. - he context used was: TripDetailPage(dependencies: [MediaQuery],
state: _TripDetailPageState#d4ab3)
Solution
Change your code to this:
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider<TripDetailBloc>(create: (BuildContext context) => TripDetailBloc()),
BlocProvider<PopUpBloc>(create: (BuildContext context) => PopUpBloc()),
],
child: Builder(
builder: (context) {
return Scaffold(
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed: () {
BlocProvider.of<TripDetailBloc>(context).add(AddTripDetailPannelEvent());
},
),
appBar: appbar(),
body: pannel(),
);
}
)
);
}
If you look closely, I have rapped your Scaffold into a widget builder.
Answered By – samezedi
Answer Checked By – David Goodson (FlutterFixes Volunteer)