0

I'm trying to run my Flutter app on my own smartphone. Everything is working fine but my smartphone can't connect to the API.

My local API is running on http://127.0.0.1:8000 (Laravel backend).

Future<List<Job>> fetchJobs() async {
  final response = await http.get(Uri.parse('http://10.0.2.2:8000/api/jobs'));
  if (response.statusCode == 200) {
    List jsonResponse = json.decode(response.body);
    return jsonResponse.map((data) => Job.fromJson(data)).toList();
  } else {
    throw Exception('Failed to load jobs');
  }
}

I'm using http://10.0.2.2:8000 because my app is currently running on an Android Emulator in Android Studio.

Now I've got the following error wenn I use my app on my smartphone:

The following LateError was thrown while handling a gesture: LateInitializationError: Field '_allUsers@32500082' has not been initialized.

My smartphone is connected to same network as my PC, I already checked this. I also tried to replace 10.0.2.2 with my private IPv4 from my PC but this doesn't work neither.

What can I do to solve this problem?

matiaslauriti
  • 7,065
  • 4
  • 31
  • 43
  • You need to use the internal IP address unless you've setup a reverse proxy in your network that would route external traffic from the internet. Also did you allow HTTP traffic in your app? Per default HTTP is disabled for Android apps. – Alexander Hoffmann Mar 05 '23 at 19:44
  • Somewhere in your code there is an `_allUsers` variable declared with `late` modifier, and you try to read its value before a value is assigned to it. – Peter Koltai Mar 05 '23 at 19:52
  • Thank you for your help: I created a new xml File: network_security_config.xml And also changed this in my AndroidMainfest.xml and also changed the IP address again but it's still not working. – Dominik Ladner Mar 05 '23 at 20:01
  • `10.0.2.2` is only used if you are running an Android emulator, see [here](https://stackoverflow.com/questions/5806220/how-to-connect-to-my-http-localhost-web-server-from-android-emulator). In case of a real device use the interal IP address as @AlexanderHoffmann suggested, [see here](https://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device). – Peter Koltai Mar 05 '23 at 20:01
  • And you need to make sure the `8000` port is open on your PC, listening to incoming traffic, not blocked by firewall etc. – Peter Koltai Mar 05 '23 at 20:02
  • @PerterKoltai my 8000 port is open I checked this. – Dominik Ladner Mar 05 '23 at 20:07
  • Can you call this API endpoint from another computer on the same network, with Postman for example? (I don't know about this way of network security config, but for me in `AndroidManifest.xml` I need ``) – Peter Koltai Mar 05 '23 at 20:21
  • I changed the network security config in AndroidManifest.xml and also try to call my API from another computer. My API isn't working but i can connect to my PC via Postman (Xampp is running on localhost) but my App wont work at all. – Dominik Ladner Mar 05 '23 at 20:46
  • So you can make a GET request with Postman the PHP backend, but with the same settings you can't make the request from your Android device / emulator? If the device is physical, is on the same network? Mobile net is switched off? – Peter Koltai Mar 05 '23 at 20:49
  • Do you have `` in your manifest? – Peter Koltai Mar 05 '23 at 20:50
  • No I can't make a Get request with Postman for the PHP backend form another PC in my network. I have in my manifest – Dominik Ladner Mar 05 '23 at 20:55
  • Until you can't make it with Postman there is no point trying from the app. Can you `ping` the backend's IP address from the other PC? What kind of response do you receive with Postman? You have to narrow it down. Is it a networking issue or PHP issue etc. – Peter Koltai Mar 05 '23 at 21:01
  • Thank you for your help. My Laravel backend is running on `http://127.0.0.1:8000`, my intern IP adress is `192.168.10.157`. So i tried to make a get Request with another notebook in the same network like this: `http://192.168.10.157:8000/api/users` but this didn't work. The onlything that works with my other computer is to ping 192.168.10.157. – Dominik Ladner Mar 05 '23 at 21:09
  • Ok, I am not familiar with `Xampp`, but normally you need to configure the ports where a web server listens to incoming traffic. The fact that it runs on port 8000 on localhost does not mean that port 8000 is exposed to the "outer world". – Peter Koltai Mar 05 '23 at 21:20
  • Try `php artisan serve --host 192.168.10.157 --port 8000`, and also have a look at [this question](https://stackoverflow.com/questions/28956911/how-can-i-access-my-laravel-app-from-another-pc), you might find it useful, I took this idea from there. – Peter Koltai Mar 05 '23 at 21:22
  • And you still need to check firewall settings, incoming traffic should be enabled for this port. – Peter Koltai Mar 05 '23 at 21:22
  • Now I can make a GET Request from my other Computer – Dominik Ladner Mar 05 '23 at 21:31
  • And from the Android device? With this setup you need a physical device on the same network, with mobile net switched off. – Peter Koltai Mar 05 '23 at 21:33
  • Everything works now, thank your for your help. I wish you all the best, you helped me a lot!! :) – Dominik Ladner Mar 05 '23 at 21:35
  • Good to hear, you are welcome! – Peter Koltai Mar 05 '23 at 21:36

0 Answers0