简体   繁体   English

Flutter 小部件内的 FOR 循环

[英]Flutter FOR loop inside widget

So essentially what I am trying to achieve is this:所以基本上我想要实现的是:

在此处输入图像描述

But apparently its not allowed in flutter.但显然它在 flutter 中是不允许的。

I'm trying to load a LIST of restaurant items and their quantity values using a FOR loop and display them unto the app with a text widget.我正在尝试使用 FOR 循环加载餐厅项目列表及其数量值,并使用文本小部件将它们显示到应用程序中。 But with the current logic that I used I need to iterate and update the variable of x and range after displaying each item name along with its quantity from their respective lists.但是使用我使用的当前逻辑,在显示每个项目名称及其各自列表中的数量之后,我需要迭代和更新xrange的变量。 The problem here is I can't use the FOR loop within widgets as you would a normal FOR loop where you can place multiple statements within the FOR loop.这里的问题是我不能像在普通的 FOR 循环中那样在小部件中使用 FOR 循环,您可以在 FOR 循环中放置多个语句

The snippet of code below is the logic I'm trying to achieve.下面的代码片段是我想要实现的逻辑。 It is similar to the code I'm implementing in flutter.它类似于我在 flutter 中实现的代码。 The FOR loop portion is the only portion I'm kind of stuck with. FOR 循环部分是我唯一坚持的部分。

Still kind of new to Flutter, so any help or guidance would be appreciated. Flutter 还是有点新的,所以任何帮助或指导将不胜感激。 Thank you!谢谢!

void main()
{
 int x =0;
  int range =0;
  
  
  
  List<String> restaurauntNames = ['Jollibee','Chowking','Mcdonalds'];
  
 List<int> orderNumber = [3,2,1];
  
 List<int> itemQuantity = [1,1,2,3,1,3];
  
 List<String> itemName= ['Jolly Spag','Chicken Joy','Peach Mango Pie','Chao Fan','Lauriat','Big Mac'];
  
 
  

  for(String i in restaurauntNames)
  {
    print(i);
    for(int z =0; z!=orderNumber[x];z++)
    {
      print(itemName[z+range]+'---------------'+ itemQuantity[z+range].toString());
    }
    range = range + orderNumber[x];
    x++;
  }
  

You can't put brace in for loop widget, I've got the same problem for quite some time and one the solution was to map the iterated list.您不能将大括号放入 for 循环小部件中,我在很长一段时间内遇到同样的问题,其中一个解决方案是 map 迭代列表。

children: [
    ...restaurantNames.map((i){
      print(i);
      for(int z = 0; z != loadedOrder.orderNumber[x];z++){
        return Text(loadedOrder.itemNames[z + range] + 'X' +loadedOrder.itemQuantity[z + range].toString());
      }
      range = range + orderNumber[x];
      x++;
    }).toList(),
  ],

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM