为什么 Flutter 应用程序在安装“ app-release. apk”时不能连接到 Internet? 但它在调试模式下正常工作

在调试模式下,一切正常。我从 API 得到答案和数据列表。但是在创建了 App-release. apk并将其安装到我的手机上之后,就再也没有互联网连接了。

这是我的代码:

ScopedModelDescendant<ReportPosViewModel>(
builder: (context, child, model) {
return FutureBuilder<List<Invoice>>(
future: model.invoices,
builder: (_,
AsyncSnapshot<List<Invoice>> snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.none:
case ConnectionState.active:
case ConnectionState.waiting:
return Center(
child:
const CircularProgressIndicator());
case ConnectionState.done:
if (snapshot.hasData) {
// Something todo
}
else if (snapshot.hasError) {
return NoInternetConnection(
action: () async {
await model.setInvoice();
await getData();
},
);
}
}
},
);
},
),
96442 次浏览

Add this to file android/app/src/main/AndroidManifest.xml after the package name:

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

Open the AndroidManifest.xml file located at ./android/app/src/main and add the following line:

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

From here:

Add the android.permission.INTERNET permission if your application code needs Internet access. The standard template does not include this tag but allows Internet access during development to enable communication between Flutter tools and a running app.

If you had put

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

in AndroidManifest.xml

And if it's not working, try checking the connectivity of the device. Mobile data or Wi-Fi on the Android device. Try using the Google Chrome browser for Google Search.

If it's not working, allow

8.8.8.8

in the DNS setting of the computer you are using.

For my project, which uses Firebase, downloading the updated google-services.json from firebase console solved the issue.