I have flutter web app which can be easily deploy to chrome browser on my PC. Upon successful deployment:
I'm looking for the way to access localhost running in chrome from my iPhone browser. My iPhone and PC both connected to the same network. I grabbed IP address of the network and tried accessing from my iPhone safari browser with the link as:
http://192.168.43.36:61867
But it's NOT working and I'm getting 'The site can't be reached' message. Is there any extra step I can perform to make flutter localhost accessible from my mobile browser or simply its impossible with flutter ?
To run a Flutter application which is connected to the localhost, on a real device, first the real device and the machine which acts as localhost should be connected on the same network.
The key point to remember, when you want to connect your Flutter application to a real device is that, both the device and PC should be connected to the same network.
While you're developing your app, Flutter doesn't really output the JS. The flutter run command launches the application using the development compiler in a Chrome browser, which means that Dart code runs directly in Chrome. And you cannot really access Chrome from another machine in the network, as it doesn't act as a server.
Using 10.0.2.2 instead of localhost or 127.0.0.1 make sense when you are using emulator. But It’s expected that you can’t connect to the localhost being not in the same network. You can share hotspot from your desktop, connect mobile to it and use your IP in this network (most probable 192.168.137.1) instead of localhost.
If you want debug only, you may use
flutter run -d web-server --web-port 8080 --web-hostname 0.0.0.0
then access http://<your-ip>:8080
** also you should ensure that your port(8080) is open.
The only way I managed to make it work on the Mac and test on the iPhone:
In your terminal, inside the project folder:
flutter build web
Building without sound null safety For more information see https://dart.dev/null-safety/unsound-null-safety
Compiling lib/main.dart for the Web... 34.5s
cd build/web
python3 -m http.server 8000
Serving HTTP on :: port 8000 (http://[::]:8000/) ... ::1 - - [28/Mar/2021 18:34:31] "GET / HTTP/1.1" 200 - ::1 - - [28/Mar/2021 18:34:31] "GET /main.dart.js HTTP/1.1" 200 - ::1 - - [28/Mar/2021 18:34:31] "GET /manifes....
Then in a new terminal tab:
~/ngrok http 8000
Forwarding https://xxxxxx.ngrok.io
or information on how to use ngrok: www.ngrok.com
While you're developing your app, Flutter doesn't really output the JS. The flutter run
command launches the application using the development compiler in a Chrome browser, which means that Dart code runs directly in Chrome. And you cannot really access Chrome from another machine in the network, as it doesn't act as a server.
You should probably compile your app in JS (AKA flutter build web
) for a regular deployment, to access it from other hosts. You could use Python's simple HTTP server to serve the app. There is no need to install any frameworks, configure anything, and writte Python code. Just make sure you have Python 3 installed and run python -m http.server 8000
from your apps build output. It will serve the app on port 8000.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With