build 메서드에서 위젯에 반복문을 사용할 일이 생겼음
// 방법 1
class Example extends StatelessWidget {
List <int> yourList = [1,2,3,4];
Widget build(BuildContext context) {
return
child: Column(
children: yourList.map((element) => Text(yourElement.toString())).toList(),
);
}
}
// 방법 2
class Example extends StatelessWidget {
List <int> yourList = [1,2,3,4];
Widget build(BuildContext context) {
return
child: Column(
children: for(yourElement in yourList) Text(yourElement.toString())
);
}
}
Widget build(BuildContext context) {
List<int> yourList = [1,2,3,4];
return
child: Column(
children: List.generate(yourList.length,(index){
return Text(yourList[index].toString());
}
);
상황에 맞게 잘 사용하면 좋을 듯