Search code examples
androidapirestflutterapk

Flutter APK Build


Goo day friends! I am new to Flutter and I have been experimenting on fetching data via REST API. On Android Studio, everything works just fine, so I suppose there's nothing wrong with my code.

Things I did to make sure my code works: 1.) Run the code in android simulator; 2.) Run the code using real device (enabling USB Debugging).

And it works just fine.

But when I build an apk file out of it and install it on my device, it no longer makes the API call (or is unable to?). I made it so, when the app runs initState(), it waits for the data to be loaded. While the data is not yet available, a CircularProgressIndicator() takes the entire screen.

If I run the app via the installed apk file, it's just CircularProgressIndicator(). Meaning, no data is being loaded and displayed. But when I run the code in AndroidStudio, the data is shown..

I am using http package. I'm not sure what I'm doing wrong, or what I'm missing.

Other things to note: I did not change anything in my AndroidManifest file, and just followed all the steps on building apk file in Flutter through the official documentation.


Solution

  • Like alexsanderfr said, this is indeed most of the time being caused by missing

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

    You shouldn't need

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    

    for REST API.

    In Flutter, when debugging you implicitly have access to internet, however in release builds, you need to explicitly declare this permission.

    update1

    Ensure you have the http-package added to your pubspec.yaml file and that it's properly indented with two spaces:

    dependencies:
      flutter:
        sdk: flutter
      cupertino_icons: ^0.1.2
      http: ^0.12.0+4