list Flutter 将项目添加到列表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/55400529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flutter add item to list
提问by Martin Seubert
I would like to add an item to a list:
我想在列表中添加一个项目:
void submitAll() async {
List<UserSearchItem> userSearchItems = [];
Firestore.instance
.collection('insta_users')
.snapshots()
.listen((data) =>
data.documents.forEach((doc){
print(data.documents.length);
User user = new User.fromDocument(doc);
UserSearchItem searchItem = new UserSearchItem(user);
userSearchItems.add(searchItem);
print(user.bio);
}));
print("Loaded");
print(userSearchItems.length);
}
But if I print the length of the list to the console, it always says, the list is 0 long...
但是如果我将列表的长度打印到控制台,它总是说,列表是 0 长......
print(userSearchItems.length);
Any suggegstions?
有什么建议吗?
Best Regards
此致
回答by Taym
I will try to give an explanation of what is happing here take a look on this code:
我将尝试解释这里发生的事情,看看这段代码:
import 'dart:async';
void main() {
List<int> userSearchItems = [];
Timer _sendTimeOutTimer;
const oneSec = Duration(seconds: 2);
_sendTimeOutTimer = Timer.periodic(oneSec, (Timer t) {
userSearchItems.add(1);
print(userSearchItems.length); // result 1 and it will be executed after 2 seconds
_sendTimeOutTimer.cancel();
});
print(userSearchItems.length); // result 0 and it will be executed first
}
The print inside asynchronous action(Timer) it will be executed after 2 seconds means after the asynchronous action ends but the one which is outside of asynchronous action(Timer) it will be executed directly without waiting 2 seconds, in your case the asynchronous action is listening to data .listen((data) =>
, so if you print the length outside of your asynchronous action you will not see the deferent because the item is not added yet.
异步操作(定时器)内的打印将在 2 秒后执行,表示异步操作结束后,但异步操作(定时器)之外的打印将直接执行,无需等待 2 秒,在您的情况下,异步操作是收听数据.listen((data) =>
,因此如果您在异步操作之外打印长度,您将看不到传送的内容,因为该项目尚未添加。
Solution: you can create function witch return Future
and then wait until it's finished then print the length.
解决方案:您可以创建函数女巫返回Future
,然后等到它完成然后打印长度。
List<UserSearchItem> userSearchItems = [];
Future<String> submitAll() async {
Firestore.instance
.collection('insta_users')
.snapshots()
.listen((data) =>
data.documents.forEach((doc){
print(data.documents.length);
User user = new User.fromDocument(doc);
UserSearchItem searchItem = new UserSearchItem(user);
userSearchItems.add(searchItem);
print(user.bio);
return 'success';
}));
}
void yourFunction() async{
await submitAll();
print("Loaded");
print(userSearchItems.length);
}
Then call yourFunction()
.
然后调用yourFunction()
。
回答by Taym
Try to add print(userSearchItems.length);
inside forEach after adding the item and you will see the real length.
print(userSearchItems.length);
添加项目后尝试在 forEach 内部添加,您将看到实际长度。