0

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.

Eric Sison
  • 199
  • 2
  • 3
  • 11
  • Your post lacks enough information to investigate the problem. In particular, it's hard to help with code issues without seeing the relevant parts of the code—a description of the code is usually not enough. Please see [How do I ask a good question?](/help/how-to-ask) and [How to create a Minimal, Reproducible Example](/help/minimal-reproducible-example). – Ryan M May 10 '20 at 07:49

2 Answers2

4

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
Community
  • 1
  • 1
Mr. Tekneurt
  • 91
  • 1
  • 3
  • Thanks. I did. But the problem still persists. One thing I noticed though, when I tried to load the generated apk file to AndroidStudio 'Analyze APK' feature, I noticed that the http package wasn't included in the final build. Some packages like FontAwesome and CupertinoIcons were included, but not the http.. Why is that? O.o – Eric Sison May 10 '20 at 09:42
  • Do you have the http-package in your pubspec.yaml and proper indentation, something like? dependencies: flutter: sdk: flutter cupertino_icons: ^0.1.2 http: ^0.12.0+4 – Mr. Tekneurt May 10 '20 at 09:56
  • Yes, Sir I have. I posted another question in this site, describing this problem, but in a smaller test app. Here's the link: https://stackoverflow.com/questions/61710099/some-package-not-included-in-final-apk-build-in-flutter – Eric Sison May 10 '20 at 10:01
0

Did you add the internet permission to the manifest? If you're accessing the internet from an Android app you need to add the internet permission over the application tag.

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