简体   繁体   中英

Flutter return widgets in for loop

My code below is working, but instead of returning multiple widgets based on the length of the list, it stops in the first round and after a lot of research and googling I understand that it stops because I'm returning a widget. So basically the for loop stops when it hits the "return". And if I don't add the "return" before the widget it return's nothing or it gives error saying that the "widget expecting a return type but nothing returning". So no "I think" I know th issue but I can't find the solution.

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

I did try with the map but it didn't work too, Here's the code for the map:

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

I think Map method of list could be best solution for this type of situation.

It is really hard to change such big code without edit, so i showed how you can do in your case.

 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(),
      )),
    );
  } 

I still tried my best to change code. i hope following code work without any error.

Moreover, you was making list two time by wrapping list with list(for loop with for loop) so removed it.

  //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')]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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