skip to Main Content

I created an flutter app with a Laravel back-end. I build the APK file for my app and installed it on to my mobile phone and connected my mobile phone to the same network as my computer with WiFi (my WiFi adapter is the same network as my computer).

When I try to use my web browser to connect my Laravel app (http://192.168.10.22:8000/api/polls), it shows expected results (for example, the poll titles). But when I try to use my app, I get empty list. Here is my code:

Future<void> fetchPolls() async {
    print('Fetching polls...');
    final response =
        await http.get(Uri.parse('http://192.168.10.22:8000/api/polls'));

    if (response.statusCode == 200) {
      print('Response body: ${response.body}'); // Log the response body
      setState(() {
        polls = jsonDecode(response.body);
        print('Polls: $polls'); // Log the polls list
      });
    } else {
      print(
          'Failed to load polls: ${response.statusCode}'); // Log the error status
      throw Exception('Failed to load polls');
    }
  }

I tried the same using Postman, http://192.168.10.22:8000/api/polls – It is ok.

Here are the things I’ve tried:

  • Added local.properties : minSdkVersion=30 to make my app compatible with android 11
  • Created rule to access Laravel app via 8000 port.
  • php artisan serve --host=0.0.0.0 --port=8000 to bind the server to 0.0.0.0

2

Answers


  1. Check

    android/app/src/main/AndroidManifest.xml
    

    You must have Internet permission to be able to connect to Internet and get data from your APIs.

    <uses-permission android:name="android.permission.INTERNET"/> <!-- Add this -->
    

    something like this:

    <manifest xmlns:android="...">
      <uses-permission android:name="android.permission.INTERNET"/>
    </manifest>
    

    After that don’t forget to do the following steps:

    flutter clean
    flutter pub get
    flutter run
    
    Login or Signup to reply.
  2. HTTP without SSL (HTTPS) are disabled by default in Flutter.

    Just add usesCleartextTraffic flagfor debug builds in AndroidManifest.xml

    <application android:usesCleartextTraffic="true"/>
    

    Follow this link for more info: https://docs.flutter.dev/release/breaking-changes/network-policy-ios-android

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search