[Flutter] Switch 대신 객체 리터럴로 사용하자


[Flutter] Switch 대신 객체 리터럴로 사용하자

비교 Bad void getCaffeine(type) { String caffeine; switch (type) { case 'Coffee': caffeine = '95 mg'; break; case 'Redbull': caffeine = '147 mg'; break; case 'Tea': caffeine = '11 mg'; break; case 'Soda': caffeine = '21 mg'; break; default caffeine = 'Not found'; } return caffeine } Good void getCaffeine(type) { String caffeine; const map = { 'Coffee': '95 mg', 'Redbull': '147 mg', 'Tea': '11 mg', 'Soda': '21 mg', }; caffeine = map[type] ?? 'Not found'; return caffeine }...


#flutter #switch #객체 #객체리터럴

원문링크 : [Flutter] Switch 대신 객체 리터럴로 사용하자