Flutter在for循环中返回小部件

时间:2020-04-05 16:26:22

标签: flutter

下面的代码可以正常工作,但是它不会根据列表的长度返回多个窗口小部件,而是在第一轮停止,经过大量研究和谷歌搜索,我知道它停止了,因为我正在返回窗口小部件。因此,基本上,for循环在到达“ return”时停止。 而且,如果我没有在小部件之前添加“ return”,则返回的内容为空,否则会出现错误,提示“小部件期望返回类型,但不返回任何内容”。因此,没有“我认为”我知道这个问题,但是我找不到解决方案。

@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');
  }

1 个答案:

答案 0 :(得分: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(),
      )),
    );
  } 

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

此外,通过将列表与list(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')]);
相关问题