Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to pop dialog in Flutter if internet connection goes off suddenly?

What I’m trying to implement is something like this,

showDialog();
await firebaseFunction();
await apiFunction();
hideDialog();

This will work properly if the internet connection is on. But what should I do if the internet connection is turned off suddenly? As if that happens, the app will get stuck on the future functions and the screen of the app will be stuck on the dialog.
I want to pop the dialog whenever the internet connection goes off.
Are there an dependencies that’ll help me do that?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

try connectivity_plus, listen to the cellular vs WiFi connection, if none, pop the dialog:

import 'package:connectivity_plus/connectivity_plus.dart';

@override
initState() {
  super.initState();

  subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
    // Got a new connectivity status!
  })
}

// Be sure to cancel subscription after you are done
@override
dispose() {
  super.dispose();

  subscription.cancel();
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading