How to Solve BlocProvider.of() Called With a Context In Flutter ?

· 2 min read
How to Solve BlocProvider.of() Called With a Context In Flutter
How to Solve BlocProvider.of() Called With a Context In Flutter

A BLoC stands as a middleman between a source of data in your app e.g an API response and widgets that need the data. It receives streams of events/data from the source, handles any required business logic, and publishes streams of data changes to widgets that are interested in them. So in this article, we are going to discuss How to Solve BlocProvider.of() Called With a Context In Flutter?

What is BLoC Pattern In Flutter?

The Business Logic Component (BLoC) pattern is a pattern created by Google and announced at Google I/O ’18. The BLoC pattern uses Reactive Programming to handle the flow of data within an app.

Purpose of the BLoc Pattern?

The main purpose of the BLoc Pattern is to make Business Logic Highly Reuseable.

What is Provider?

The provider helps to inject your Logic into the application easily.

How to Solve BlocProvider.of() Called With a Context In Flutter?

User needs to make changes in code snippet like the below:

BlocProvider.of<LoginListingBloc>(context).dispatch(LoginEvent(loginInfoModel: testLogin));

to

_loginListingBloc.dispatch(LoginEvent(loginInfoModel: testLogin));

For all others who come here for the error message: Make sure you always specify the types and don’t omit them:

BlocProvider<YourBloc>(
    create: (context) => YourBloc()
    child: YourWidget()
);

and also for

BlocProvider.of<YourBloc>(context);

Conclusion :

In this article, We have been through How to Solve BlocProvider.of() Called With a Context In Flutter?

Thanks for Reading !!!

Keep Learning !!! Keep Fluttering !!!

Do share your Suggestion/Feedback for the same.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply