how to detect if no internet connection in flutter code example
Example 1: flutter check internet connection
import 'package:connectivity/connectivity.dart';
var connectivityResult = await (Connectivity().checkConnectivity());
if (connectivityResult == ConnectivityResult.mobile) {
} else if (connectivityResult == ConnectivityResult.wifi) {
}
Example 2: check if internet connected flutter
The connectivity plugin states in its docs that it only provides information if
there is a network connection, but not if the network is connected to the
Internet
Note that on Android, this does not guarantee connection to Internet. For
instance, the app might have wifi access but it might be a VPN or a hotel WiFi
with no access. You can use
import 'dart:io';
...
try {
final result = await InternetAddress.lookup('google.com');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
print('connected');
}
} on SocketException catch (_) {
print('not connected');
}