flutter - 应用程序在调试模式下工作,但不在发布模式下
问题描述
应用程序的发布模式无法从 url 获取数据,因此屏幕仅显示“正在加载...”
我正在使用package:http/http.dart
包来获取数据。
这是我的State
班级的样子:
class HomeState extends State<Home>{
Future<List<Coin>> getCoinList() async {
var url = '<some_url>';
var response = await http.get(url);
var jsonData = json.decode(response.body);
//iterate through jsonData, create a list and return
}
Future<void> updateData() async{
setState((){});
}
@override
Widget build(BuildContext context) {
return RefreshIndicator(child: FutureBuilder(
future: getCoinList(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.data == null) {
return Center(
child: Container(
child: Text("Loading..."),
),
);
} else {
return ListView.builder(
itemCount: snapshot.data.length,
itemBuilder: (BuildContext context, int position) {
String sign = '-';
MaterialColor signColor = Colors.red;
if(double.parse(snapshot.data[position].percent_change_1h) > 0.0){
sign = '+';
signColor = Colors.green;
}
return Card(
child: ListTile(
title: Text(snapshot.data[position].name,style: TextStyle(fontWeight: FontWeight.bold),),
subtitle: Text(snapshot.data[position].price_usd,style: TextStyle(color: signColor),),
trailing: Text(sign+snapshot.data[position].percent_change_1h+"%",style: TextStyle(color: signColor),),
),
);
});
}
}), onRefresh: updateData);
}
}
请详细解释代码有什么问题以及如何修复它。谢谢你。
解决方案
getCoinList()
返回更新的数据,但您实际上需要设置状态以使用新数据,例如
setState () {
coindata = getCoinList();
}
如果您不更新状态对象,则不会重建列表。
推荐阅读
- python - Python - T-SQL 语句在 SQL Server 中工作,但是当我在 Python 中使用它时,它一直出错
- arduino - arduino 操纵杆库,不要停止操纵杆
- actions-on-google - action.devices.traits.MediaState 问题
- html - 为什么应用于我的列的边框样式被部分遮挡?
- python - 在python中获取剪贴板二进制数据
- machine-learning - 为什么我们不需要在多元线性回归中进行特征缩放
- javascript - 如何正确使用 querySelectorAll 和 addEventListener
- shell - /busybox/sh:语法错误:使用 Tekton 替换错误
- excel - SSRS 单元自动合并
- cytoscape.js - 如何获得用于创建弯曲点的段距离和段权重的准确值?