Update dart.md

added enum class and added one example of it
This commit is contained in:
Roshan-pcy authored and GitHub committed 2024-06-02 20:22:49 +05:30
1 parent 52d1ad634a
commit 2a72a11fd4
1 file changed
+38
+38
View File
@@ -664,3 +664,41 @@ userObject?.userName?.toString()
// The preceeding code returns null and never calls toString() if either userObject or userObject.userName is null
```
### enum in dart
```dart
defination: An enum (short for "enumeration") is a special data type that enables a variable to be a set of predefined constants. Enums are used to define variables that can only take one out of a small set of possible values. They help make code more readable and less error-prone by providing meaningful names to these sets of values.
// Define the enum
enum TrafficLight {
red,
yellow,
green
}
// A function that prints a message based on the traffic light state
void printTrafficLightMessage(TrafficLight light) {
switch (light) {
case TrafficLight.red:
print('Stop!');
break;
case TrafficLight.yellow:
print('Get ready...');
break;
case TrafficLight.green:
print('Go!');
break;
}
}
void main() {
// Example usage of the enum
TrafficLight currentLight = TrafficLight.green;
// Print the message for the current traffic light state
printTrafficLightMessage(currentLight);
}
```