From 2a72a11fd43af933c872abbe93424b543282e76b Mon Sep 17 00:00:00 2001 From: Roshan-pcy Date: Sun, 2 Jun 2024 20:22:49 +0530 Subject: [PATCH] Update dart.md added enum class and added one example of it --- source/_posts/dart.md | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/source/_posts/dart.md b/source/_posts/dart.md index 3aaa793..ab11327 100644 --- a/source/_posts/dart.md +++ b/source/_posts/dart.md @@ -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); +} + + +``` + +