【问题标题】:Flutter return widgets in for loop在 for 循环中颤振返回小部件
【发布时间】:2020-04-05 16:26:22
【问题描述】:

我下面的代码正在运行,但不是根据列表的长度返回多个小部件,而是在第一轮停止,经过大量研究和谷歌搜索后,我知道它停止是因为我正在返回一个小部件。所以基本上 for 循环在遇到“返回”时停止。 如果我没有在它返回的小部件之前添加“返回”,或者它给出错误说“小部件期望返回类型但没有返回”。所以没有“我认为”我知道这个问题,但我找不到解决方案。

@override
  Widget build(BuildContext context) {
    for (var allAttributes in widget.allAttributes) {
      //print(allAttributes.name);
      bool attributeCheck;
      if(widget.attributes.length > 0){
        for(var attributes in widget.attributes){
          if(allAttributes.id == attributes.attributeId){
            return Row(
              children: <Widget>[
                new Container(
                    alignment: Alignment(-1.0, -1.0),
                    child: Padding(
                      padding: const EdgeInsets.only(bottom: 10.0, right: 10.0),
                      child: Text(
                        allAttributes.name + ':',
                        style: TextStyle(
                            color: Colors.black,
                            fontSize: 20,
                            fontWeight: FontWeight.w600),
                      ),
                    )),
                DropdownButton<Attributes>(
                  hint: Text("Select item"),
                  value: selectedUser,
                  onChanged: (Attributes Value) {
                    setState(() {
                      selectedUser = Value;
                    });
                  },
                  items: widget.attributes.map((Attributes attributes) {
                    return DropdownMenuItem<Attributes>(
                      value: attributes,
                      child: Row(
                        children: <Widget>[
                          SizedBox(
                            width: 10,
                          ),
                          Text(
                            attributes.value,
                            style: TextStyle(color: Colors.black),
                          ),
                        ],
                      ),
                    );
                  }).toList(),
                ),
              ],
            );
          }
        }
      }
    }

    return Text('Nothing');
  }

我确实尝试过使用地图,但它也不起作用,这是地图的代码:

@override
  Widget build(BuildContext context) {
    widget.allAttributes.map((AllAttributes allAttributes) {
      //print(allAttributes.name);
        widget.attributes.map((Attributes attributes){
          return Row(
            children: <Widget>[
              new Container(
                  alignment: Alignment(-1.0, -1.0),
                  child: Padding(
                    padding: const EdgeInsets.only(bottom: 10.0, right: 10.0),
                    child: Text(
                      allAttributes.name + ':',
                      style: TextStyle(
                          color: Colors.black,
                          fontSize: 20,
                          fontWeight: FontWeight.w600),
                    ),
                  )),
              DropdownButton<Attributes>(
                hint: Text("Select item"),
                value: selectedUser,
                onChanged: (Attributes Value) {
                  setState(() {
                    selectedUser = Value;
                  });
                },
                items: widget.attributes.map((Attributes attributes) {
                  return DropdownMenuItem<Attributes>(
                    value: attributes,
                    child: Row(
                      children: <Widget>[
                        SizedBox(
                          width: 10,
                        ),
                        Text(
                          attributes.value,
                          style: TextStyle(color: Colors.black),
                        ),
                      ],
                    ),
                  );
                }).toList(),
              ),
            ],
          );
        }).toList();
    }).toList();

    return Text('Nothing');
  }

【问题讨论】:

  • 你需要构建一个小部件数组然后返回它
  • 请问您能提供一些代码吗?谢谢

标签: flutter


【解决方案1】:

我认为列表的 Map 方法可能是这种情况的最佳解决方案。

这么大的代码在不经过编辑的情况下很难改变,所以我展示了你的情况。

 List<int> _data = [1, 2, 3, 4, 5, 6];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(
          child: Column(
        children: _data.map((e) {
          return Text(e.toString());
        }).toList(),
      )),
    );
  } 

我仍然尽力更改代码。我希望以下代码可以正常工作。

此外,您通过使用列表(for 循环和 for 循环)包装列表来制作列表两次,因此将其删除。

  //print(allAttributes.name);
  return Column(
        children:
    widget.attributes.length>0? widget.attributes.map((Attributes attributes){
      return Row(
        children: <Widget>[
          new Container(
              alignment: Alignment(-1.0, -1.0),
              child: Padding(
                padding: const EdgeInsets.only(bottom: 10.0, right: 10.0),
                child: Text(
                  allAttributes.name + ':',
                  style: TextStyle(
                      color: Colors.black,
                      fontSize: 20,
                      fontWeight: FontWeight.w600),
                ),
              )),
          DropdownButton<Attributes>(
            hint: Text("Select item"),
            value: selectedUser,
            onChanged: (Attributes Value) {
              setState(() {
                selectedUser = Value;
              });
            },
            items: widget.attributes.map((Attributes attributes) {
              return DropdownMenuItem<Attributes>(
                value: attributes,
                child: Row(
                  children: <Widget>[
                    SizedBox(
                      width: 10,
                    ),
                    Text(
                      attributes.value,
                      style: TextStyle(color: Colors.black),
                    ),
                  ],
                ),
              );
            }).toList(),
          ),
        ],
      );
    }).toList(): [Text('Nothing')]);

【讨论】:

  • 感谢您的帮助 恐怕您的代码不起作用,出现了一些错误。无论如何,我已经添加了我自己的代码,我尝试过的地图也不起作用。如果您可以检查该代码,我将不胜感激。对于列表,我要比较的是两种类型的列表,所以我没有制作两个列表。谢谢
  • 当您尝试并且不需要两次包装列表映射时会发生什么。并解释当你尝试我的解决方案时究竟发生了什么。
  • 修复语法错误后,显示“无”小部件。
  • @user2682025 我更新了我的答案检查它,如果这个答案不起作用,还添加 print(allAttributes.name) 输出。
  • 它仍然显示“无”小部件。而对于 print(allAttributes.name),它会打印列表中的所有名称。
猜你喜欢
  • 2020-04-13
  • 1970-01-01
  • 2020-12-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-10-26
  • 1970-01-01
  • 2018-11-29
相关资源
最近更新 更多