4

I am not able to access the outer for loop counter in inner for loop

Any idea on how to do this ?

class buildsubcategories extends StatelessWidget {
  List<cate.Categories> scat;

  buildsubcategories(this.scat);
  @override
  Widget build(BuildContext context) {
    return Column(
      children: <Widget>[
        for (int i = 0; i < scat.length; i++) Text(scat[i].categoryname),
        Column(
          children: <Widget>[
            for (int j = 0; j < scat.length; j++)
              Text(scat[i].subcategory[j]['subcategoryname'].toString())
          ],
        )
      ],
    );
  }
```}


Expected Result : Able to access the variable i in the inner for loop
1
  • import 'package:flutter/material.dart'; class Categories { Categories( {@required this.categoryname, @required this.categoryImage, @required this.subcategory}); String categoryname; String categoryImage; List<dynamic> subcategory; Categories.fromMap(Map<dynamic, dynamic> map) { this.categoryname = map['name']; this.categoryImage = map['imageurl']; this.subcategory = map['subcategory']; } } Commented Jul 7, 2019 at 7:29

3 Answers 3

8

You don't have a nested loop here. Please see the comments I added below:

  children: <Widget>[
    // this creates scat.length many Text elements here
    for (int i = 0; i < scat.length; i++) Text(scat[i].categoryname),
    // there is only one column that comes after the scat.length many Text elements 
    Column(
      children: <Widget>[
        // this creates scat.length many elements inside the Column
        for (int j = 0; j < scat.length; j++)
          Text(scat[i].subcategory[j]['subcategoryname'].toString())
      ],
    )
  ],

Here's how you can create categories in a nested loop:

  children: <Widget>[
    // note the ... spread operator that enables us to add two elements 
    for (int i = 0; i < scat.length; i++) ...[ 
      Text(scat[i].categoryname),
      Column(
        children: <Widget>[
          // this creates scat.length many elements inside the Column
          for (int j = 0; j < scat.length; j++)
            Text(scat[i].subcategory[j]['subcategoryname'].toString())
        ],
      )
    ]
  ],

Note that to add two elements in each loop iteration we had to put the two into a list and unwrap it with the ... spread operator.

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

Comments

0

I don't think the syntax you're using is proper as you're trying to create a for loop inside a return statement. Instead you can try this:

  @override
  Widget build(BuildContext context) {
    List<Widget> myList = List.generate(10, (index){
      var outerText = [Text("OuterText")];
      return Column(
        children: outerText..addAll(List.generate(10, (index){
          Text("Inner Text");
        })),
      );
    });
    return Column(
      children: myList
    );
  }

2 Comments

It's a new Dart feature: medium.com/flutter-community/…
Wow. Thanks for bringing this into my attention! Saw your answer looks neat. Will start using that myself!
0
 children: <Widget>[
// this creates scat.length many Text elements here
for (int i = 0; i < scat.length; i++) Text(scat[i].categoryname),
// there is only one column that comes after the scat.length many Text elements 
Column(
  children: <Widget>[
    // this creates scat.length many elements inside the Column
    for (int j = 0; j < scat.length; j++)
      Text(scat[i].subcategory[j]['subcategoryname'].toString())
  ],
)

],

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.