Last active
July 24, 2023 14:44
switch statement better
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
enum MesEnum { | |
janeiro('Janeiro'), | |
fevereiro('Fevereiro'), | |
marco('Março'), | |
abril('Abril'), | |
maio('Maio'), | |
junho('Junho'), | |
julho('Julho'), | |
agosto('Agosto'), | |
setembro('Setembro'), | |
outubro('Outubro'), | |
novembro('Novembro'), | |
dezembro('Dezembro'); | |
final String description; | |
const MesEnum(this.description); | |
} | |
String pegarMes(int mes) => switch (mes) { | |
1 => MesEnum.janeiro.description, | |
2 => MesEnum.fevereiro.description, | |
3 => MesEnum.marco.description, | |
4 => MesEnum.abril.description, | |
5 => MesEnum.maio.description, | |
6 => MesEnum.junho.description, | |
7 => MesEnum.julho.description, | |
8 => MesEnum.agosto.description, | |
9 => MesEnum.setembro.description, | |
10 => MesEnum.outubro.description, | |
11 => MesEnum.novembro.description, | |
12 => MesEnum.dezembro.description, | |
_ => 'Mês inválido', //Valor padrão, substitui o default | |
}; | |
void main() { | |
int mes = 1; | |
String result = pegarMes(mes); | |
print(result); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment