'Flutter HttpServer cannot access from other computers on the LAN network
I want to implement feature that enables send data to the Flutter android app through http server. For example I want to make vibration on the app when my node.js app tasks running on Windows ends.
Here example from Write HTTP clients & servers | Dart and this works on localhost:8080 on the android. But does not work on LAN network, e.g. from this Windows machine thought the android LAN IP address "http://192.168.0.193:8080/". I did google and searched Stack overflow, and found I needed --web-port 8080 --web-hostname 0.0.0.0
when run Flutter, but still this doesn't work, this shows me 192.168.0.193 refused to connect.
The android IP is confirmed through settings > wifi > wifi preference > advanced > IP addresses
and I'm sure the IP itself is correct.
So, I have no idea why I cannot connect to the app through the local IP address. Greatly appreciate your help, thanks.
import 'dart:io';
Future main() async {
var server = await HttpServer.bind(
InternetAddress.loopbackIPv4,
8080,
);
print('Listening on ${server.address.toString()}:${server.port} ');
await for (HttpRequest request in server) {
request.response.write('Hello, world!');
await request.response.close();
}
}
flutter run -d F5122 --web-port 8080 --web-hostname 0.0.0.0
Solution 1:[1]
Using InternetAddress.anyIPv4 instead of InternetAddress.loopbackIPv4 solved for me.
Solution 2:[2]
My solution was this:
var server = await HttpServer.bind(
InternetAddress("192.168.1.X"),
8080,
);
Replace the value X with the number of your server computer
This way you can access from another computer.
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 | Insta Mfk |
Solution 2 | DomingoMG |