0
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)

2 Answers 2

1

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;

Sign up to request clarification or add additional context in comments.

Comments

1

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()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.