user15608457
user15608457

Reputation:

How do i get a alert dialog when internet is off

Future checkConnectivity (BuildContext context) async {
  try {
    final result = await InternetAddress.lookup('google.com');
    if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
      print(result[0]);
    }
  } on SocketException catch (_) {
    //error alert dialog
  }
}

When internet is off i want to show a alert dialog to the user that there is no internet.

Upvotes: 0

Views: 34

Answers (1)

Luis A. Chaglla
Luis A. Chaglla

Reputation: 600

You could use this package: https://pub.dev/packages/connectivity

and do something like this:

import 'package:connectivity/connectivity.dart';

...

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

  subscription = Connectivity().onConnectivityChanged.listen((ConnectivityResult result) {
    if (result == ConnectivityResult.none) {
      showDialog(...);
    }
  });
}

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

  subscription.cancel();
}

Upvotes: 1

Related Questions