Reputation: 113
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);
}```
Upvotes: 2
Views: 674
Reputation: 113
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)
Upvotes: 1