hasInternet method

Future<bool> hasInternet()

Implementation

Future<bool> hasInternet() async {
  final connectivityResult = await _connectivity.checkConnectivity();
  if (connectivityResult == ConnectivityResult.none) return false;
  if (kIsWeb) {
    try {
      final r = await http
          .get(Uri.parse('https://clients3.google.com/generate_204'))
          .timeout(const Duration(seconds: 5));
      return r.statusCode == 204;
    } catch (_) {
      return false;
    }
  } else {
    try {
      final client = HttpClient()
        ..connectionTimeout = const Duration(seconds: 5);
      final req = await client.getUrl(
        Uri.parse('https://clients3.google.com/generate_204'),
      );
      req.followRedirects = false;
      final res = await req.close().timeout(const Duration(seconds: 5));
      return res.statusCode == 204;
    } catch (_) {
      return false;
    }
  }
}