![](/img/trans.png)
[英]Flutter app not fetching any data from REST API after building it with flutter build apk --split-per-abi --no-sound-null-safety
[英]Flutter: After build APK, data from API doesn't appear
我正在嘗試創建一個應用程序來顯示來自 API 的一些數據。 問題是,雖然 App 在使用模擬器或智能手機的調試模式下運行良好,但沒有顯示任何錯誤。 如果我構建 APK,該應用程序不會下載數據,或者至少不會顯示它們。 我該如何解決這個問題?
更多細節:
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,
);
}));
}
}
}
您面臨的問題是因為當您處於調試模式時,調試模式具有默認具有 Internet 權限的 Android 清單文件,但src
中有主文件夾,其中有另一個清單文件,您沒有獲得互聯網許可。 正如 MePo 所說,您應該授予互聯網許可
<uses-permission android:name="android.permission.INTERNET"/>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.