I have a list which is under Build, it works for outside of ListView.builder, but the loop inside of ListView.builder will only return first element, why? thanks for advices!
Widget build(BuildContext context) {
List eventList = [
TimelineModel(
eventTitle: 'first',),
TimelineModel(
eventTitle: 'second'),
];
return GetBuilder<TimelineController>(builder: (controller) {
for(var e in eventList){
print('???${e.eventTitle}'); // here loops work
}
return Scaffold(
body: Container(
child: Stack(
children: [
ListView.builder(
itemBuilder: (context, index) {
for (var event in eventList)
print('------${event.eventTitle}------'); // here loop will only return first element
),
],
),
),
);
}
);
CodePudding user response:
You probably want to use it like this:
ListView.builder(
itemCount: eventList.length,
itemBuilder: (context, index) {
print('------${eventList[index].eventTitle}------');
return Text(eventList[index].eventTitle);
}
),
CodePudding user response:
Add itemCount
inside ListView.builder()
Widget build(BuildContext context) {
List eventList = [
TimelineModel(
eventTitle: 'first',),
TimelineModel(
eventTitle: 'second'),
];
return GetBuilder<TimelineController>(builder: (controller) {
for(var e in eventList){
print('???${e.eventTitle}'); // here loops work
}
return Scaffold(
body: Container(
child: Stack(
children: [
ListView.builder(
itemCount: eventList.length,
itemBuilder: (context, index) {
for (var event in eventList)
print('------${event.eventTitle}------'); // here loop will only return first element
),
],
),
),
);
}
);