Your Flutter app works on the emulator. You install it on your real phone for the FYP demo, hit login, and get Connection refused or a Dio timeout. This fails for almost every student the first time, and the reason is simple: on a physical device, localhost is the phone itself — not your laptop.
Here is the fix that works, step by step.
Why the emulator tricks you
- Android emulator: 10.0.2.2 points back to your laptop's localhost.
- Physical device: 10.0.2.2 means nothing. 127.0.0.1 / localhost points to the phone.
- So the same base URL that worked in the emulator can never work on a real device.
Step 1: Put the phone and laptop on the same Wi-Fi
Both devices must be on the same network. University Wi-Fi often blocks device-to-device traffic (client isolation) — if the steps below fail on campus Wi-Fi, use your phone's hotspot and connect the laptop to it instead. That one change fixes a lot of "nothing works" cases.
Step 2: Find your laptop's local IP
Windows: run ipconfig and look for the IPv4 Address under your Wi-Fi adapter, e.g. 192.168.1.42. Linux/Mac: ip addr or ifconfig.
Step 3: Serve Laravel on the network, not only localhost
php artisan serve --host=0.0.0.0 --port=8000
--host=0.0.0.0 makes the server listen on all network interfaces. Without it, Laravel only accepts connections from the laptop itself.
Test it before touching Flutter: open http://192.168.1.42:8000/api/... (one of your GET routes) in your phone's browser. If the phone browser cannot load it, Flutter will not either — check the firewall step below.
Step 4: Point Flutter at the laptop IP
Do not hardcode the IP in ten files. Keep one config:
// lib/core/api_config.dart
class ApiConfig {
// Emulator: http://10.0.2.2:8000/api
// Physical device: your laptop's LAN IP
static const String baseUrl = 'http://192.168.1.42:8000/api';
}
And use it everywhere:
final dio = Dio(BaseOptions(baseUrl: ApiConfig.baseUrl));
When you switch between emulator and phone, you change one line.
Step 5: Allow cleartext HTTP on Android
Your local server is HTTP, not HTTPS. Modern Android blocks cleartext traffic by default, which shows up as a vague network error in Flutter even when the server is reachable.
In android/app/src/main/AndroidManifest.xml, inside the application tag, add: android:usesCleartextTraffic="true"
This is fine for a local FYP demo. A deployed production API should be HTTPS, and then you remove this.
Step 6: Let it through the Windows firewall
If the phone browser timed out in Step 3, Windows Firewall is the usual blocker. Allow PHP / the 8000 port for Private networks: Windows Defender Firewall, Allow an app through firewall, allow PHP — or add an inbound rule for TCP port 8000 on the Private profile only. Do not open it on Public networks.
Quick checklist when it still fails
- Connection refused instantly: Laravel not running with --host=0.0.0.0, or wrong IP.
- Timeout: different networks, campus Wi-Fi client isolation, or firewall.
- Works in phone browser, fails in app: cleartext HTTP blocked — check Step 5.
- Worked yesterday, broken today: laptop IP changed (DHCP). Re-check ipconfig.
- 401 / 419 errors: connection is fine — now it is an auth issue. For APIs, use token auth (Sanctum) and make sure the route is in api.php, not web.php.
For demo day
Your IP will probably be different in the university lab. Reach 30 minutes early, connect to the network, re-check the IP, update ApiConfig.baseUrl, rebuild, and test login on the actual phone you will present with. Most "the app stopped working in the viva" stories are just a changed IP.
Top comments (0)