List의 멤버함수

하상현·2023년 11월 23일
0

.forEach()

변수안의 요소들을 전부 반복문 돌리고 싶을때

리스트변수명.forEach((value){
  print('${value} 출력');
});

.map()

변수안의 요소들을 전부 원하는 형태로 바꾸고 싶을 때
근데 결과 값의 데이터타입이 Iterable(반복가능한)이라는 것을 명심하자
그래서 Iterable를 List로 다시 바꿔준다(.toList()멤버함수 활용)

List<String> myNumber = ['1','2','3'];

myNumber.map((e){
  return Text(e);
}).toList();

//결과
[Text('1'),Text('2'),Text('3')]

.where()

필터를 걸어서 조건에 해당하는 요소만 남기고 싶을때
Iterable이 리턴된다.
예)

var myScore = [96,92,94,95,73,98,78,82,96,48];

myScore.where((e){
  return e > 80;  //true면 남고,false면 제거
}).toList();

.map()과 .where()함께 쓰기

var myScore = [96,92,94,95,73,98,78,82,96,48];

myScore.where((element){
  return element > 80;
}).map((element){
  retrun Text('$element');
}).toList()

//결과 
[Text('96'),Text('92'),Text('94'),Text('95'),Text('98'),Text('82'),Text('96'),]
//80점 초과의 점수들이 List<int>에서 List<Widget>으로 바뀜

Block body Function에서 Arrow Function(Expression Body Function)으로 바꾸기

//Block body Function
myScore.where((e){
  return e > 80;
}).map((e){
  retrun Text('$e');
}).toList(),

//Arrow Function
myScore
  .where((e) => e > 80)
  .map((e) => Text('$e)
  .toList(),

0개의 댓글