Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access flutter localhost from real mobile browser

I have flutter web app which can be easily deploy to chrome browser on my PC. Upon successful deployment:

  1. Console

enter image description here

  1. Web browser(Chrome)

enter image description here

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 ?

like image 808
iAkshay Avatar asked Jun 04 '20 13:06

iAkshay


People also ask

How to run a flutter application on the localhost?

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.

How to connect your flutter application to a real device?

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.

Can I run flutter code in another computer?

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.

Why can't I connect to my localhost when using emulator?

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.


3 Answers

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.

like image 75
LemonTea Avatar answered Oct 17 '22 13:10

LemonTea


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

like image 35
luisdemarchi Avatar answered Oct 17 '22 14:10

luisdemarchi


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.

like image 6
madhead - StandWithUkraine Avatar answered Oct 17 '22 12:10

madhead - StandWithUkraine