我在Dart中有以下for循环:
Locations allLocations(AsyncSnapshot<Results> snap) {
for (var i = 0; i < snap.data.locationList.length; i++) {
return snap.data.locationList[i];
}
}
我的目标是遍历要通过快照获取的位置列表,然后返回每个值。不幸的是,Dart分析器告诉我,此函数并不以return语句结尾。好吧,我不确定在此示例中我在做什么错。
感谢您的帮助!
答案 0 :(得分:0)
尝试一下:
Locations allLocations(AsyncSnapshot<Results> snap) {
List returnedList = new List();
for (var i = 0; i < snap.data.locationList.length; i++) {
returnedList.add(snap.data.locationList[i]);
}
return returnedList;
}
答案 1 :(得分:0)
一定不要在每个索引处返回值,否则,该函数将仅在第一个索引处返回,并且不会经历完整的迭代。相反,您应该在循环外返回完整列表。
List<Locations> mList= new List();
Locations allLocations(AsyncSnapshot<Results> snap) {
for(var i in snap.data.locationList){
mList.add(return snap.data.locationList[i]);
}
return snap.data.locationList;
}
答案 2 :(得分:0)
我想你想要这样的东西
Stream<int> allInts(List<int> list) async* {
for (var i = 0; i < list.length; i++) {
yield list.elementAt(i);
}
}
当我使用这个
allInts(<int>[1, 3, 5, 7, 9]).listen((number) {
print(number);
});
控制台:
I/flutter (24597): 1
I/flutter (24597): 3
I/flutter (24597): 5
I/flutter (24597): 7
I/flutter (24597): 9