简体   繁体   中英

How can I use for loop in flutter?

return Scaffold(
      
      body: SingleChildScrollView(
          child: Column(
        children: <Widget>[
          Stack(
            children: <Widget>[
              Column(
                children: <Widget>[
                  NewsBlock(
                    size: size,
                    dataList: dataList,
                    imgNum: 1,
                    titleNum: 11,
                    urlNum: 21,
                  ),
                  NewsBlock(
                    size: size,
                    dataList: dataList,
                    imgNum: 2,
                    titleNum: 12,
                    urlNum: 22,
                  ),
                  NewsBlock(
                    size: size,
                    dataList: dataList,
                    imgNum: 3,
                    titleNum: 13,

                  ),
                  NewsBlock(
                    size: size,
                    dataList: dataList,
                    imgNum: 4,
                    titleNum: 14,
                  ),
                ],
              )
            ],
          ),
        ],
      )),
    );

I want to make that long code to short with for loop. But I don't know how to use for loop in flutter.. I think I can reduce the length of code by putting i in imgNum, titleNum, and urlNum and increasing i. How can I do that? (btw there are 10 news blocks and I didn't write urlNum for all news blocks yet because typing all them made me feel like a fool)

You can use for loop like this:

  final maxSize = ???;
  final imgStartIndex = 1;
  final titleStartIndex = 11;
  final urlStartIndex = 21;

  return Scaffold(
    body: SingleChildScrollView(
        child: Column(
      children: <Widget>[
        Stack(
          children: <Widget>[
            Column(
              children: <Widget>[
                for (int i = 0; i < maxSize; i++)
                  NewsBlock(
                    size: size,
                    dataList: dataList,
                    imgNum: imgStartIndex + i,
                    titleNum: titleStartIndex + i,
                    urlNum: urlStartIndex + i,
                  ),
              ],
            )
          ],
        ),
      ],
    )),
  );

I do not know the structure of your data, so you should guess yourself how to calculate maxSize ;

To your question, you can do like this

children: [
              for (var i = 0; i < List.length; i++)
                NewsBlock()
          ],

Other way to do is to use map in the list data

children: newsData.map((item)=>NewsBlock(
                    size: item.size,
                    dataList: item.datalist,
                    imgNum: item.imgNum,
                    titleNum: item.titleNum,
                  )).toList()

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