반응형
enum Fruits {
apple('red', 1000, true),
banana('yellow', 1500, false),
grape('purple', 2000, true),
;
final String color;
final int price;
final bool favorite;
const Fruits(this.color, this.price, this.favorite);
// 특정 색깔인 과일 찾기
factory Fruits.getByColor(String fruitColor) {
return Fruits.values
.firstWhere((value) => value.color == fruitColor);
}
}
print(Fruits.apple.color); // output : red
print(Fruits.banana.price); // output : 1500
// 빨간색인 과일
print(Fruits.getByColor('red')); // output : Fruits.apple
// 보라색인 과일의 가격
print(Fruits.getByColor('purple').price); // output : 2000
// 노란색인 과일을 좋아하는지
print(Fruits.getByColor('yellow').favorite); // output : false
print(Fruits.values.byName('grape')); // output : Fruits.grape
반응형
'Flutter' 카테고리의 다른 글
[Flutter] GridView 커스텀 하기 (flutter_staggered_grid_view) (0) | 2024.08.19 |
---|---|
[Flutter] Getx로 보는 MVC패턴과 MVVM패턴의 차이 (0) | 2024.08.18 |
[Flutter] Parse Issue (Xcode): Module 'sqflite' not found 에러 해결 (0) | 2024.03.20 |
[Flutter] Tabbar Widget 탭바 위젯 (0) | 2024.03.20 |
[Flutter] Sidebar, Side Navigation 구현하기 (sidebarx) (1) | 2024.02.05 |