[英]Flutter: After build APK, data from API doesn't appear
I'm trying to create an App that shows some data coming from an API.我正在尝试创建一个应用程序来显示来自 API 的一些数据。 The problem is that, while App works well in debugging mode using emulator or smartphone, without showing any errors.问题是,虽然 App 在使用模拟器或智能手机的调试模式下运行良好,但没有显示任何错误。 If I build APK the App doesn't download data or at least it doesn't show them.如果我构建 APK,该应用程序不会下载数据,或者至少不会显示它们。 How can I solve this issue?我该如何解决这个问题?
Some more details:更多细节:
import 'dart:convert';
import 'package:apiitest2/models/Obj.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class Home extends StatefulWidget {
@override
_HomeState createState() => _HomeState();
}
class _HomeState extends State<Home> {
List<Obj> dataDef = List<Obj>();
void getData() {
List<Obj> dataDef0 = List<Obj>();
http.get(Uri.encodeFull("https://jsonplaceholder.typicode.com/posts"),
headers: {"Accept": "application/json"}).then((resp) {
List data = json.decode(resp.body);
for (var item in data) {
Obj obj = Obj(item["userId"], item["id"], item["title"], item["body"]);
dataDef0.add(obj);
}
setState(() {
dataDef = dataDef0;
});
});//.catchError((onError){});
}
@override
void initState() {
super.initState();
getData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: CustomScrollView(slivers: <Widget>[
SliverAppBar(
backgroundColor: Colors.red,
floating: false,
pinned: true,
expandedHeight: 200.0,
flexibleSpace: FlexibleSpaceBar(
centerTitle: true,
title: Text(
"Title",
style: TextStyle(
color: Colors.black87,
fontSize: 20.0,
fontWeight: FontWeight.bold,
),
),
background: Container(
color: Colors.amber,
)
),
),
body()
]
)
);
}
Widget body() {
if (dataDef.isEmpty)
return SliverToBoxAdapter(
child: SizedBox(
height: MediaQuery.of(context).size.height - 200,
child: Center(
child: Container(
height: 70,
width: 70,
child: CircularProgressIndicator()
)
),
)
);
else {
return SliverList(delegate:
SliverChildBuilderDelegate((BuildContext context, int index) {
if (index > dataDef.length - 1) return null;
return Container(
child: Text(dataDef[index].title),
height: 50,
);
}));
}
}
}
The issue you are facing because when you are in the debugging mode, the debug mode has the Android manifest file which has the internet permission by default, but there is the main folder in the src
in which there is another manifest file where you have not given the Internet permission.您面临的问题是因为当您处于调试模式时,调试模式具有默认具有 Internet 权限的 Android 清单文件,但src
中有主文件夹,其中有另一个清单文件,您没有获得互联网许可。 As MePo said that you should give the internet permission正如 MePo 所说,您应该授予互联网许可
<uses-permission android:name="android.permission.INTERNET"/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.