Flutter & Dart: ENUM example

Last updated on October 19, 2020 The Frog Loading... Post a comment

An example of using enumeration in Dart and Flutter.

The code:

// Declare enum
enum Gender {
  Male,
  Female
}

// Make a class
class Person {
  final String name;
  final int age;
  final Gender gender;
  
  Person(this.name, this.age, this.gender); 
}

// Create an instance
final personA = Person("John Doe", 40, Gender.Male); 

void main(){
  if(personA.gender == Gender.Male){
    print("Hello gentleman!");
  } else {
    print("Hello lady!");
  }
}

Output:

Hello gentleman!

Related Articles

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x