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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With