'Dart InternetAddress.lookup failed for the local network, but works for google.com
Dart unable to connect the server running on the local network. In below code I am trying to find weather or not my host is reachable before making the API request.
Future<String> networkMonitor() async {
try {
var result = await InternetAddress.lookup('http://192.168.0.104:8000');
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
return 'connected';
}
}on SocketException catch (_) {
return 'Unable to connect server';
}
}
void main() async {
var a = await networkMonitor();
print(a);
}```
Solution 1:[1]
Inside the InternetAddress.lookup() function we have to give it the host address without the http or port number, in above case it will be InternetAddress.lookup('192.168.0.104'). Other wise it will throw an error SocketException: Failed host lookup: '192.168.0.104:8000' (OS Error: Name or service not known, errno = -2)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | ibex |