Use Map Instead of Switch Statement for Lookup
Don't do this
String getCaffeine(String type) {
switch (type) {
case 'Coffee':
return '95 mg';
case 'Redbull':
return '147 mg';
case 'Tea':
return '11 mg';
case 'Soda':
return '21 mg';
default:
return 'Not found';
}
}
void main() {
print(getCaffeine('Coffee')); // Output: 95 mg
print(getCaffeine('Soda')); // Output: 21 mg
print(getCaffeine('Juice')); // Output: Not found
}
Do this
String getCaffeine(String type) {
const caffeineMap = {
'Coffee': '95 mg',
'Redbull': '147 mg',
'Tea': '11 mg',
'Soda': '21 mg',
};
return caffeineMap[type] ?? 'Not found';
}
void main() {
print(getCaffeine('Coffee')); // Output: 95 mg
print(getCaffeine('Soda')); // Output: 21 mg
print(getCaffeine('Juice')); // Output: Not found
}