Ternary operator inside loop generated 'ListView'
You want to add Divider
after three and six, But using ternary operator, you are replacing the ListTile
widget with Divider
.
val? true:false
, means here only one will be return. What you need to just check the index and add divider only after three
and six
, if
statement is perfect for this as @Josteve mentioned on his answer. While in children
can only be widget instead of list, he is using Column
to merge Divider
and ListTile
, you can use spread operator
instead.
ListView(
children: [
for (int i = 0; i < 8; i++) ...[
ListTile(title: Text(items[i])),
if (i == 2 || i == 5) const Divider()
]
],
),
You can check more about spread-operator and use cases.
Try this:
for (int i = 0; i < 8; i++) ...[
ListTile(title: Text(items[i])),
if (i == 2 || i == 5) const Divider()
]