Home > Software design >  Displaying array with index in Dart Flutter
Displaying array with index in Dart Flutter

Time:08-01

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: Code Result

enter image description here 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

CodePudding user response:

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,
                 )
           ],
         ),
       )
  • Related