For loop 如何使用Dart从for循环返回值
对于Dart中的循环,我有以下内容:For loop 如何使用Dart从for循环返回值,for-loop,flutter,dart,return-value,For Loop,Flutter,Dart,Return Value,对于Dart中的循环,我有以下内容: Locations allLocations(AsyncSnapshot<Results> snap) { for (var i = 0; i < snap.data.locationList.length; i++) { return snap.data.locationList[i]; } } 位置所有位置(异步快照快照){ 对于(var i=0;i
Locations allLocations(AsyncSnapshot<Results> snap) {
for (var i = 0; i < snap.data.locationList.length; i++) {
return snap.data.locationList[i];
}
}
位置所有位置(异步快照快照){
对于(var i=0;i
我的目标是通过快照遍历位置列表,然后返回每个值。不幸的是,Dart分析器告诉我,这个函数不是以return语句结束的。嗯,我不确定在这个例子中我做错了什么
谢谢你的帮助 试试这个:
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;
}
位置所有位置(异步快照快照){
List returnedList=新列表();
对于(var i=0;i
您不能在每个索引处返回值,否则,函数将仅在第一个索引处返回,不会经过完整的迭代。相反,您应该在循环外返回完整的列表
List<Locations> mList= new List();
List mList=new List();
位置所有位置(异步快照快照){
for(snap.data.locationList中的变量i){
mList.add(返回snap.data.locationList[i]);
}
返回snap.data.locationList;
}
我想你想要这样的东西
Stream<int> allInts(List<int> list) async* {
for (var i = 0; i < list.length; i++) {
yield list.elementAt(i);
}
}
什么是地理位置?您可以共享位置类Location类由两个参数组成,例如
纬度
和经度
。我正在加载一个包含位置信息的json文件,因此我有一个加载数据的快照。现在我想在谷歌地图上显示标记。为了显示每个标记,我需要遍历数据列表。函数调用只能返回一次。多次返回是没有意义的,所以不清楚你想要实现什么。那么,你真正想要实现的是什么?我将采用易卜拉欣的方法。我想要实现的是,读取列表中存储的所有数据并将其显示在屏幕上。我不能只使用ListView.builder或类似工具,因为这些是表示mapI上点的坐标。我不太确定我是否理解您的解决方案。Mysnap.data.locationList
已经是一个列表。我不想返回完整的列表,但要返回列表中的每个值。我需要列表中的每个值,因为我想显示每个值。如果在循环中使用return
,那么return将在第一个索引中退出循环,我希望您能得到它。我得到了这一点。这不能解决我的问题。我想返回列表中的每个值snap.data.locationList
。当我将代码复制到IDE中时,它会抱怨我在mList.add()中使用了return语句。另外,snap.data.locationList[i]
中的i
不是int
类型,而是来自类型Location
,因为您在snap.data.locationList中写入var i。所以可能有一些事情是错误的。我写这篇文章是为了澄清这个概念。我没有在IDE中写这个。另外,如果你想显示一些东西,你必须从这里返回一个列表,并在构建函数中使用listview.count。Mysnap.data.locationList
已经是一个列表。我不想返回完整的列表,但要返回列表中的每个值。我需要列表中的每个值,因为我想显示每个值。这有点正确。我想我可以接受。谢谢
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