Flutter 延迟颤振更新列表项
我有一个从数据库加载的项目列表,问题是每个项目都需要从服务器加载一个字符串,我找不到一种方法使该字符串得到更新 逻辑Flutter 延迟颤振更新列表项,flutter,Flutter,我有一个从数据库加载的项目列表,问题是每个项目都需要从服务器加载一个字符串,我找不到一种方法使该字符串得到更新 逻辑 显示数据库中的位置列表(完成) 显示每个项目当前时间(问题) 屏幕截图 代码注释 Padding( padding: const EdgeInsets.symmetric(vertical: 1.0, horizontal: 4.0), child: ListView.builder( itemCount: locations.length,
注释
Padding(
padding: const EdgeInsets.symmetric(vertical: 1.0, horizontal: 4.0),
child: ListView.builder(
itemCount: locations.length,
itemBuilder: (context, index) {
WorldTime instance = WorldTime(name: locations[index].name, country: locations[index].country, wikipedia: locations[index].wikipedia, location: locations[index].location, flag: locations[index].flag);
// need to use await but async/await is not allowed under `Widget build(BuildContext context)`
instance.getTime();
var time = instance.time;
return Card(
child: ListTile(
onTap: () {},
title: Text('${locations[index].location}'),
subtitle: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(locations[index].country),
SizedBox(height: 5.0),
Text('$time'), // <-- show updated time (currently only show first load `null` and never gets updated).
],
),
leading: CircleAvatar(
backgroundImage: AssetImage('assets/${locations[index].flag}'),
),
),
);
},
),
),
使用FutureBuilder调用instance.getTime(),因为它返回一个Future。将
文本(“$time”)
替换为以下内容:
FutureBuilder(
future:instance.getTime(),
生成器:(上下文,快照){
if(snapshot.connectionState==connectionState.done&&instance.time!=null){
返回文本(instance.time.toString());
}
返回文本(“无数据”);
},
),
由于
getTime
返回void
并将time
分配给WorldTime
实例,因此在if
条件中使用instance
。谢谢,但它只显示没有数据
不会将其更新到时间。getTime可能返回null。您可以发布getTime()
的签名吗?instance
是未来吗?是的,它是未来,让我为您更新我的问题更新了……。编辑了我的答案,这应该可以解决您的问题。
class WorldTime {
String location; // location name for the UI
String time; // the time in that location
String flag; // url to an asset flag icon
String country;
String wikipedia;
String name; // name of the location to get data from server
bool isDaytime; // true or false if day time or not
WorldTime({this.location, this.country, this.wikipedia, this.flag, this.name});
Future<void> getTime() async {
var apiUrl = Uri.https('example.com', 'api/timezone', {'name': '$name'});
// Await the http get response, then decode the json-formatted response.
var response = await http.get(apiUrl);
if (response.statusCode == 200) {
var jsonResponse = convert.jsonDecode(response.body) as Map<String, dynamic>;
// get properties from data
DateTime dateTime = DateTime.now().toUtc();
String offset = jsonResponse['data']['offset'].substring(1, 3);// 07 (hour)
String positive = jsonResponse['data']['offset'].substring(0, 1); // determine is positive or negative offset
// create Datetime object
String now1 = DateFormat('yyyy-MM-dd HH:mm:ss').format(dateTime);
DateTime now = DateTime.parse(now1);
if(positive == '+') {
now = now.add(
Duration(
hours: int.parse(offset),
minutes: int.parse(jsonResponse['data']['offset'].substring(4, 6)) // 00 (minutes)
)
);
} else {
now = now.subtract(
Duration(
hours: int.parse(offset),
minutes: int.parse(jsonResponse['data']['offset'].substring(4, 6)) // 00 (minutes)
)
);
}
print(now); //2021-05-25 09:57:08.000
// set the time property
isDaytime = now.hour > 5 && now.hour < 18 ? true : false;
time = DateFormat.jm().format(now);
} else {
print('Request failed with error: ${response.statusCode}.');
isDaytime = false;
time = 'Could not get time data';
}
}
}