Implementation
Future<bool> get hasInternetFast async {
final socketHosts = [
{'host': '8.8.8.8', 'port': 53}, // Google DNS
{'host': '1.1.1.1', 'port': 53}, // Cloudflare DNS
{'host': '208.67.222.222', 'port': 53}, // OpenDNS
{'host': '8.8.4.4', 'port': 53}, // Google Secondary DNS
];
socketHosts.shuffle(Random());
for (final hostInfo in socketHosts) {
Socket? socket;
try {
socket = await Socket.connect(
hostInfo['host'] as String,
hostInfo['port'] as int,
timeout: Duration(seconds: 3),
);
return true;
} catch (e) {
continue;
} finally {
socket?.destroy();
}
}
return false;
}