0
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

1 Answer 1

1

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,
                 )
           ],
         ),
       )
Sign up to request clarification or add additional context in comments.

2 Comments

Why no curly braces after for loop?
Curly braces are not used inside the build method, instead of that you just simply use indentation.

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.