0

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?

Mayur Agarwal
  • 1,448
  • 1
  • 14
  • 30
  • @Boaz No, as I needed to check for internet connection while processing and not before processing. :) – Mayur Agarwal Nov 10 '21 at 10:59
  • 1
    The linked post includes solutions for all scenarios. In fact, it also includes the accepted answer to this post. Consider closing this question as a duplicate. – Boaz Nov 10 '21 at 11:41

1 Answers1

1

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) {
    if (result == ConnectivityResult.none) {
    hideDailog();
    showToast("No internet connection");
  }
  });
}

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

  subscription.cancel();
}
Mayur Agarwal
  • 1,448
  • 1
  • 14
  • 30
Jim
  • 6,928
  • 1
  • 7
  • 18