I am getting the error while trying to integrate the flutter_bloc in flutter project.
Error: Could not find the correct Provider<InternetBloc> above this BlocBuilder<InternetBloc, InternetState> Widget
Here is the code sample i am using.
internet_state.dart
abstract class InternetState{}
class InitialInternetState extends InternetState{}
class LostConnectionInternetState extends InternetState{}
class GainConnectionInternetState extends InternetState{}
internet_event.dart
abstract class InternetEvent{}
class LostConnectionEvent extends InternetEvent{}
class GainConnectionEvent extends InternetEvent{}
internet_bloc.dart
class InternetBloc extends Bloc<InternetEvent, InternetState>{
final Connectivity _connectivity = Connectivity();
StreamSubscription? streamSubscription;
InternetBloc() : super(InitialInternetState()){
on<GainConnectionEvent>((event, emit) => emit(GainConnectionInternetState()));
on<LostConnectionEvent>((event, emit) => emit(LostConnectionInternetState()));
_connectivity.onConnectivityChanged.listen((event) {
if(event == ConnectivityResult.mobile || event == ConnectivityResult.wifi){
add(GainConnectionEvent());
} else {
add(LostConnectionEvent());
}
});
}
@override
Future<void> close() {
streamSubscription?.cancel();
return super.close();
}
}
main.dart
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatelessWidget {
const MyHomePage({super.key, required this.title});
final String title;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(title),),
body: SafeArea(
child: Center(child: BlocBuilder<InternetBloc, InternetState>(
builder: (context, state) {
return const Text("hello");
},
)),
),);
}
}
Eroor i am getting.
======== Exception caught by widgets library =======================================================
The following ProviderNotFoundException was thrown building MediaQuery(MediaQueryData(size: Size(392.7, 783.3), devicePixelRatio: 2.8, textScaler: no scaling, platformBrightness: Brightness.light, padding: EdgeInsets.zero, viewPadding: EdgeInsets.zero, viewInsets: EdgeInsets.zero, systemGestureInsets: EdgeInsets(29.8, 24.0, 29.8, 8.0), alwaysUse24HourFormat: false, accessibleNavigation: false, highContrast: false, onOffSwitchLabels: false, disableAnimations: false, invertColors: false, boldText: false, navigationMode: traditional, gestureSettings: DeviceGestureSettings(touchSlop: 8.0), displayFeatures: [])):
Error: Could not find the correct Provider above this BlocBuilder<InternetBloc, InternetState> Widget
This happens because you used a BuildContext
that does not include the provider
of your choice. There are a few common scenarios:
-
You added a new provider in your
main.dart
and performed a hot-reload.
To fix, perform a hot-restart. -
The provider you are trying to read is in a different route.
Providers are "scoped". So if you insert of provider inside a route, then
other routes will not be able to access that provider. -
You used a
BuildContext
that is an ancestor of the provider you are trying to read.Make sure that BlocBuilder<InternetBloc, InternetState> is under your MultiProvider/Provider.
This usually happens when you are creating a provider and trying to read it immediately.For example, instead of:
Widget build(BuildContext context) { return Provider<Example>( create: (_) => Example(), // Will throw a ProviderNotFoundError, because `context` is associated // to the widget that is the parent of `Provider<Example>` child: Text(context.watch<Example>().toString()), ); }
consider using
builder
like so:Widget build(BuildContext context) { return Provider<Example>( create: (_) => Example(), // we use `builder` to obtain a new `BuildContext` that has access to the provider builder: (context, child) { // No longer throws return Text(context.watch<Example>().toString()); } ); }
If none of these solutions work, consider asking for help on StackOverflow:
https://stackoverflow.com/questions/tagged/flutter
The relevant error-causing widget was:
SafeArea SafeArea:file:///Users/../Projects/flutterTest/bloc_test/lib/main.dart:37:13
When the exception was thrown, this was the stack:
#0 Provider._inheritedElementOf (package:provider/src/provider.dart:343:7)
#1 Provider.of (package:provider/src/provider.dart:293:30)
#2 ReadContext.read (package:provider/src/provider.dart:649:21)
#3 _BlocBuilderBaseState.initState (package:flutter_bloc/src/bloc_builder.dart:130:36)
#4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:5602:55)
#5 ComponentElement.mount (package:flutter/src/widgets/framework.dart:5447:5)
... Normal element mounting (4 frames)
#9 Element.inflateWidget (package:flutter/src/widgets/framework.dart:4326:16)
#10 Element.updateChild (package:flutter/src/widgets/framework.dart:3831:20)
#11 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#12 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#13 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#14 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#15 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6743:14)
#16 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#17 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#18 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#19 StatelessElement.update (package:flutter/src/widgets/framework.dart:5547:5)
#20 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#21 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#22 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#23 StatelessElement.update (package:flutter/src/widgets/framework.dart:5547:5)
#24 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#25 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#26 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#27 StatelessElement.update (package:flutter/src/widgets/framework.dart:5547:5)
#28 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#29 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#30 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#31 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#32 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#33 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#34 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#35 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#36 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#37 Element.updateChildren (package:flutter/src/widgets/framework.dart:3964:32)
#38 MultiChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6896:17)
#39 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#40 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#41 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#42 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#43 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#44 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#45 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#46 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#47 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#48 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#49 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#50 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#51 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#52 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#53 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#54 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#55 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#56 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#57 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#58 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#59 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#60 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#61 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#62 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#63 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6743:14)
#64 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#65 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#66 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#67 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#68 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#69 SingleChildRenderObjectElement.update (package:flutter/src/widgets/framework.dart:6743:14)
#70 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#71 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#72 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#73 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#74 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#75 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#76 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#77 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#78 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#79 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#80 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#81 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#82 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#83 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#84 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#85 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#86 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#87 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#88 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#89 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#90 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#91 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#92 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#93 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#94 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#95 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#96 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#97 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#98 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#99 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#100 ProxyElement.update (package:flutter/src/widgets/framework.dart:5800:5)
#101 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#102 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#103 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:5634:11)
#104 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#105 StatefulElement.update (package:flutter/src/widgets/framework.dart:5657:5)
#106 Element.updateChild (package:flutter/src/widgets/framework.dart:3815:15)
#107 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:5496:16)
#108 Element.rebuild (package:flutter/src/widgets/framework.dart:5187:7)
#109 BuildOwner.buildScope (package:flutter/src/widgets/framework.dart:2895:19)
#110 WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:984:21)
#111 RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:457:5)
#112 SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1325:15)
#113 SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1255:9)
#114 SchedulerBinding.scheduleWarmUpFrame.<anonymous closure> (package:flutter/src/scheduler/binding.dart:978:7)
#118 _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)
(elided 3 frames from class _Timer and dart:async-patch)
====================================================================================================
I don’t want to use global provider like MultiProvider.
I tried
wrap your MaterialApp in
BlocProvider(
create: (context) => InternetBloc(),)
2
Answers
You should create InternetBloc somewhere and provide it.
and MultiProvider is not global by itself, it is just multi.
More info here: https://bloclibrary.dev/#/flutterbloccoreconcepts?id=blocprovider
So, if you don’t want to use global provider, it will looks something like this:
To use a BlocBuilder, you need to put a BlocProvider in the Widget tree. It’s best practice to put it as deep as possible. In your case just put the BlocProvider above the BlocBuilder.
Also the MultiProvider / MultiBlocProvider is not a global Provider. It is just a convienent widget to provide multiple Providers/BlocProviders.