简体   繁体   English

Flutter APK 构建

[英]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.我是 Flutter 的新手,我一直在尝试通过 REST API 获取数据,在 ZE84E32B9390CDB46DDB 上一切正常。 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;我为确保我的代码工作所做的事情: 1.) 在 android 模拟器中运行代码; 2.) Run the code using real device (enabling USB Debugging). 2.) 使用真机运行代码(启用 USB 调试)。

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?).但是,当我从中构建一个 apk 文件并将其安装在我的设备上时,它不再进行 API 调用(或者无法调用?)。 I made it so, when the app runs initState() , it waits for the data to be loaded.我这样做了,当应用程序运行initState()时,它会等待加载数据。 While the data is not yet available, a CircularProgressIndicator() takes the entire screen.虽然数据尚不可用,但CircularProgressIndicator()会占用整个屏幕。

If I run the app via the installed apk file, it's just CircularProgressIndicator() .如果我通过安装的 apk 文件运行应用程序,它只是CircularProgressIndicator() Meaning, no data is being loaded and displayed.意思是,没有数据被加载和显示。 But when I run the code in AndroidStudio, the data is shown..但是当我在 AndroidStudio 中运行代码时,会显示数据..

I am using http package.我正在使用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 .其他需要注意的事项:我没有更改我的AndroidManifest文件中的任何内容,只是按照官方文档在 Flutter 中构建 apk 文件的所有步骤进行操作。

Like alexsanderfr said, this is indeed most of the time being caused by missing就像alexsanderfr说的,这确实是大部分时间都是失踪造成的

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

You shouldn't need你不应该需要

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

for REST API.对于 REST API。

In Flutter, when debugging you implicitly have access to internet, however in release builds, you need to explicitly declare this permission.在 Flutter 中,调试时您隐式可以访问 Internet,但是在发布版本中,您需要显式声明此权限。

update1更新1

Ensure you have the http-package added to your pubspec.yaml file and that it's properly indented with two spaces:确保您已将 http-package 添加到您的 pubspec.yaml 文件中,并且使用两个空格正确缩进:

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

Did you add the internet permission to the manifest?您是否将 Internet 权限添加到清单? If you're accessing the internet from an Android app you need to add the internet permission over the application tag.如果您从 Android 应用程序访问互联网,则需要在应用程序标签上添加互联网权限。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM