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
Spread the love
Tagged : / /

Leave a Reply

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