Search code examples
flutterandroid-studiogonetwork-programmingandroid-manifest

How to resolve connection issues when running a Flutter app on a physical Android device?


I have developed a Flutter application with a Go backend, which runs as expected when using an emulator. However, I encounter issues when attempting to run it on a physical Android device. Specifically, the app fails to connect to the backend server using the IP address specified in the code.

The expected behavior is for the app to connect to the backend server and perform API calls successfully, as it does in the emulator. Unfortunately, when I try accessing "http://1##.###.#.#:8080" directly on my phone's browser, I can't establish a connection. This makes me suspect there might be a network configuration or permission issue preventing the app from communicating with my backend when running on a physical device.

Code:

Below are relevant snippets from my Flutter project:

class AppConstants{
  static const String BASE_URL = "http://1##.###.#.#:8080";
}

And the network-related configuration in the AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.flutter_golang_yt">
   <application
        android:label="Todogo"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/LaunchTheme"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>
</manifest>


Solution

  • Perhaps you should deploy your backend on a service like Heroku. It seems to me that your backend runs on the emulator because it's able to connect to localhost on your computer. However, your physical mobile phone cannot connect to localhost.