在forEach循环中填充列表之前,我的异步调用正在返回 [英] My async call is returning before list is populated in forEach loop
问题描述
我有一个例程,该例程从设备获取文件名列表,然后读取文件以构建列表.但是,调用例程始终返回零项.我打印了文件名,所以我知道它们存在,但是,在我读取文件之前,似乎异步返回了.进行HTTP调用时,我使用了类似的代码.但是,这里的某件事导致例程返回列表,即使列表尚未完成.也许,我可能在错误的时间称呼它?我在这里叫retrieveItems:
I have a routine which gets a list of filenames from the device, then reads the file(s) to build a list. However, the calling routine always returns with zero items. I print the filenames, so I know they exist, however, it appears that the async is returning before I read the files. I used similar code when making an HTTP call. But, something here is causing the routine to return the list even though it hasn't completed. Perhaps, it is possible that I am calling it at the wrong time? I am calling retrieveItems here:
@override
void initState() {
super.initState();
retrieveItems();
}
最终,我将有一个刷新按钮,但是现在,我只希望列表中填充文件中的数据...
Eventually I will have a refresh button, but for now I'd simply like the list to populate with the data from the files...
被叫人
Future<List<String>> readHeaderData() async {
List<String> l = new List();
List<String> files = await readHeaders(); // Gets filenames
files.forEach((filename) async {
final file = await File(filename);
String contents = await file.readAsString();
User usr = User.fromJson(json.decode(contents));
String name = usr.NameLast + ", " + usr.NameFirst;
print(name);
l.add(name);
}
return l;
来电者
Caller
void retrieveItems() async {
LocalStorage storage = new LocalStorage();
await storage.readHeaderData().then((item) {
try {
if ((item != null ) &&(item.length >= 1)) {
setState(() {
users.clear();
_users.addAll(item);
});
} else {
setState(() {
_users.clear();
final snackbar = new SnackBar(
content: new Text('No users found.'),
);
scaffoldKey.currentState.showSnackBar(snackbar);
});
}
} on FileNotFoundException catch (e) {
print(e.toString()); //For debug only
setState(() {
_users.clear();
});
});
}
});
推荐答案
此代码
Future<List<String>> readHeaderData() async {
List<String> l = new List();
List<String> files = await readHeaders(); // Gets filenames
files.forEach((filename) async {
final file = await File(filename);
String contents = await file.readAsString();
User user = User.fromJson(json.decode(contents));
String name = user.NameLast + ", " + user.NameFirst;
print(name);
l.add(name);
}
return l;
}
返回列表l
,然后处理asyc forEach(...)
回调
returns the list l
and then processes the asyc forEach(...)
callbacks
如果将其更改为
Future<List<String>> readHeaderData() async {
List<String> l = new List();
List<String> files = await readHeaders(); // Gets filenames
for(var filename in files) { /// <<<<==== changed line
final file = await File(filename);
String contents = await file.readAsString();
User user = User.fromJson(json.decode(contents));
String name = user.NameLast + ", " + user.NameFirst;
print(name);
l.add(name);
}
return l;
}
在处理所有文件名之前,该函数不会返回.
the function will not return before all filenames are processed.
files.forEach((filename) async {
表示您可以在回调中使用await
,但是forEach
不在乎(filename) async {...}
返回什么.
means that you can use await
inside the callback, but forEach
doesn't care about what (filename) async {...}
returns.
这篇关于在forEach循环中填充列表之前,我的异步调用正在返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!