简体   繁体   中英

Displaying array with index in Dart Flutter

Padding(
  padding: const EdgeInsets.all(8.0),
  child: Column(
           crossAxisAlignment: CrossAxisAlignment.start,
           mainAxisAlignment: MainAxisAlignment.center,
           children: <Widget>[
             for (var i = 0;i < snapshot.data[0]["meanings"][index]["definitions"].length;i++)
               for (var item in snapshot.data[0]["meanings"][index]["definitions"])
                 Text(
                   (i + 1).toString() + ". " + item["definition"],
                   textAlign: TextAlign.left,
                 )
           ],
         ),
       )

This is the result the code gives: 代码结果

在此处输入图像描述 I would like something like:

  1. A party...
  2. A hairdo
  3. Something that.... ...

List should end at 7 which is the length of the array

In order to get rid of unnecessary repetition you just simply have to get rid of your nested for loop, which is totally redundant here. Access your item with loop index - snapshot.data[0]["meanings"][index]["definitions"][i]["definition"]

Padding(
  padding: const EdgeInsets.all(8.0),
  child: Column(
           crossAxisAlignment: CrossAxisAlignment.start,
           mainAxisAlignment: MainAxisAlignment.center,
           children: <Widget>[
             for (var i = 0;i < snapshot.data[0]["meanings"][index]["definitions"].length;i++)
                 Text(
                   (i + 1).toString() + ". " + snapshot.data[0]["meanings"][index]["definitions"][i]["definition"],
                   textAlign: TextAlign.left,
                 )
           ],
         ),
       )

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