Chapter 4 - Why enum has to be declared outside main function

Hi the enum concept is introduced in Switch condition.
The example enum:

enum Weather {
  sunny,
  snowy,
  cloudy,
  rainy,
}

has to be declared outside main function. It has error if I put it inside main function, is there a simple reason to explain this?

In Dart, a class cannot be defined in a function. An enum is a class.

2 Likes

Hi Laura, thank you. Really simple reason, don’t know why I cannot think of that :smile:

1 Like

The upcoming Dart Apprentice: Fundamentals book chapter 5 “Control Flow” addresses this question now. Thank you.