I know there are several similar questions listed, but I can not resolve it after I tried some solutions from previous questions. 🙁
I want to do a network check and show a dialog if it has no network connection, so I did the following:
class App extends StatefulWidget {
const App({super.key});
@override
State<App> createState() => _AppState();
}
class _AppState extends State<App> {
@override
Widget build(BuildContext context) {
return MultiBlocProvider(
providers: [
BlocProvider(
lazy: false,
create: (context) => NetworkBloc()
..add(
const StartMonitor(),
),
),
],
child: MaterialApp.router(
routerConfig: router,
builder: (context, child) {
return SubWidget(
child: child,
);
},
),
);
}
}
//try to create a sub-widget
class SubWidget extends StatelessWidget {
const SubWidget({super.key, this.child});
final Widget? child;
@override
Widget build(BuildContext context) {
return UpgradeAlert(
navigatorKey: router.routerDelegate.navigatorKey,
child: Builder(//try to add build
builder: (context) {
return BlocListener<NetworkBloc, NetworkState>(
listener: (context, state) async {
if (state is NetworkFailure) {
await showDialog(
context: context,
builder: (context) {
return const AlertDialog(
title: Text('No Network'),
);
});
}
},
child: child,
);
}),
);
}
}
the network connection check is fine, but the problem is with showing the dialog. I know there might be some problems with the context, so I tried to create a sub-widget or use a Builder (), but the problem persists.
3
Answers
I just use this package connectivity_plus and it work for me
The
context
parameter in thelistener
callback is shadowing thecontext
from thebuild
method. You can change the name of thecontext
parameter in thelistener
callback to something else, or if you’re not planning to use it change it to_
.The parameter
builder
ofMaterialApp
works as a wrapper above the inner widgets or inhereted widgets it creates (MediaQuery, Navigator, etc) so in that context those widgets don’t exist yet (they are below). Your widget tree is something like this:This is from the builder parameter
What you need is to use your navigator key in your dialog context, and from there use
navigatorKey.currentContext
;There is no need to make a
Builder
wrapper. That’s the reason UpdateDialog from that package asks you to pass the navigator key, to do exactly the same you’re trying and avoid depending on other contexts