Flutter 试图在ListView的子级内部调用Future函数
正在尝试在Flatter中创建应用程序使用情况应用程序。 在下面的代码中,我调用了ListView子元素内部的appNameProvider()函数。并将appName返回给子级。问题是,它打印的不是该函数的值,而是“Future”的实例Flutter 试图在ListView的子级内部调用Future函数,flutter,Flutter,正在尝试在Flatter中创建应用程序使用情况应用程序。 在下面的代码中,我调用了ListView子元素内部的appNameProvider()函数。并将appName返回给子级。问题是,它打印的不是该函数的值,而是“Future”的实例 List<AppUsageInfo> infoList = await AppUsage.getAppUsage(startDate, endDate); setState(() { _infos = infoList;})
List<AppUsageInfo> infoList = await AppUsage.getAppUsage(startDate, endDate);
setState(() {
_infos = infoList;});
Future<String> appNameProvider(pn) async {
Application apps = await DeviceApps.getApp(pn);
return apps.appName;
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text("Usage Stats"),
),
body: ListView.builder(
itemCount: _infos.length,
itemBuilder: (context, index) {
return ListTile(
title: Text(_infos[index].packageName.toString()),
subtitle: Text(_infos[index].usage.toString()),
trailing: Text("${appNameProvider(_infos[index].packageName.toString())}"),
);
}),
List使用FutureBuilder调用appNameProvider,因为它返回未来
trailing:FutureBuilder(
future:appNameProvider(_infos[index].packageName.toString()),
生成器:(上下文,快照){
if(snapshot.hasData&&snapshot.connectionState==connectionState.done&&snapshot.data!=null){
返回文本(snapshot.data);
}
返回文本(“无数据”);
},
),
未来类型函数需要等待
接收结果。