Categories
Dart Programming

Dart – How to loop an enum

Dart provides .values to get list of values in an enum. Since it is a list, we can use any method of List data type.

In this post we make a demo to loop through all the values of an enum.

enum Animal { Dog, Cat, Chicken, Elephant }

void main() {
  Animal.values.forEach((name) {
    print(name);
  });
}

Output:

Animal.Dog
Animal.Cat
Animal.Chicken
Animal.Elephant

1 reply on “Dart – How to loop an enum”

*An interesting discussion is worth comment. I think that you should write more on this topic, it might not be a taboo subject but generally people are not enough to speak on such topics. To the next. Cheers

Leave a Reply

Your email address will not be published. Required fields are marked *