I have defined all my blocs and injects them using getIt, now I want to call this particular event BlocProvider.of(context).add(GetMerchantCategoryEvent()); in the initState immediately the page is launched but whenever I run the app, I am getting this error that says BlocProvider.of() called with a context that does not contain a BeneficiaryBloc.
Here is what I did
class PageSelectParish extends StatefulWidget {
const PageSelectParish({
super.key,
required this.title,
required this.payType,
});
final PayType payType;
final String title;
@override
State<PageSelectParish> createState() => _PageSelectParishState();
}
class _PageSelectParishState extends State<PageSelectParish>
with SingleTickerProviderStateMixin {
bool loading = true;
final GlobalKey<ScaffoldState> _scaffoldKey = GlobalKey();
//declare merchant category list
List<MerchantCategoryModel> merchantCategories = [];
//errorState
bool inErrorState = false;
@override
void initState() {
super.initState();
// call get merchant event
BlocProvider.of<BeneficiaryBloc>(context).add(GetMerchantCategoryEvent());
}
@override
Widget build(BuildContext context) {
final tabList = [
if (widget.payType != PayType.paygroup) ...[
TabModel(title:'All parishes', child: TabviewAllParishesWidget(payType: widget.payType)),
TabModel(title:'Favorite parishes', child: TabviewRecentParishesAndGroups(payType: widget.payType)),
] else ...[
TabModel(title:'From parishes', child: TabviewAllParishesWidget(payType: widget.payType)),
TabModel(title: 'Favorite groups', child: TabviewRecentParishesAndGroups(payType: widget.payType)),
TabModel(title: 'ArchDiocese', child: TabviewDiocesanWidget(payType: widget.payType)),
const TabModel(title: 'National', child: PageNationalGroup())
],
];
return BlocProvider(create:(context)=>sl<BeneficiaryBloc>(),
child: BlocListener<BeneficiaryBloc, BeneficiaryState>(
listener: (context, state) {
pp(state.runtimeType);
if (state is GetMerchantCategoriesLoading) {
setState(() {
if (merchantCategories.isEmpty) {
loading = true;
} else {
//isMoreLoading = true;
}
inErrorState = false;
});
} else if (state is GetMerchantCategoryError) {
setState(() {
if (merchantCategories.isEmpty) {
inErrorState = true; //initialize inErrorState to true
loading = false;
}
});
} else if (state is GetMerchantCategoriesLoaded) {
//complete refreshcontroller
//refreshController.refreshCompleted();
setState(() {
loading = false;
merchantCategories = state.merchantCategories;
});
}
},
child:Scaffold()