Dart Enum and Composition - How to Organize Code?
My Introduction to Enum and Composition
Yesterday I started with Dart basics. Today I looked at more advanced topics: Enums and Composition. At first I thought "what are these for?" but once I saw the use cases, it clicked.
Live Demo: Dart Enum and Composition
Try enum and composition usage interactively:
What is an Enum? Why Do We Use It?
Think of enums like a menu. For example, you're making a pizza ordering app and pizzas have sizes: Small, Medium, Large. Instead of writing "small", "medium", "large" as strings every time, you can define and use an enum.
A Practical Example: Color Selection
I first tried it with color selection:
enum Colors {
red,
blue,
white
}
void main() {
whichColor(Colors.blue);
}
void whichColor(Colors color) {
switch (color) {
case Colors.white:
print("White selected");
break;
case Colors.red:
print("Red selected");
break;
case Colors.blue:
print("Blue selected");
break;
}
}Since we sent Colors.blue in this code, it will print "Blue selected" to the screen.
The Good Things About Enums
- Lower chance of errors: You won't misspell like "bluee" or "blu"
- More readable code: Writing
Colors.blueis more descriptive than the string "blue" - IDE support: VS Code or Android Studio gives you auto-completion
Usage Example in Flutter
Imagine you're doing state management in an app:
enum AppState {
loading,
success,
error,
emptyContent
}
void checkState(AppState state) {
switch (state) {
case AppState.loading:
// Show loading widget
break;
case AppState.success:
// Show data
break;
case AppState.error:
// Show error message
break;
case AppState.emptyContent:
// "No content found" message
break;
}
}Very clean and understandable, right?
Composition - Has-A Relationship
The composition topic was a bit confusing at first, but here's how I understood it: You use another class inside a class. That is, a "has-a" relationship.
Real Life Example
A person has information. Also, their car has information. But when defining the car, we don't want to rewrite all the person's information:
class PersonInfo {
late int id;
late String name;
late int age;
PersonInfo({
required this.id,
required this.name,
required this.age
});
}
class CarInfo {
PersonInfo person; // This is important!
late String carName;
late String plate;
CarInfo({
required this.person,
required this.carName,
required this.plate
});
}
void main() {
var person1 = PersonInfo(
age: 25,
name: "Ahmet",
id: 1233
);
var car1 = CarInfo(
person: person1,
carName: "Toyota Corolla",
plate: "34 ABC 123"
);
print("Person named ${car1.person.name} has a car: ${car1.carName}");
print("Plate: ${car1.plate}");
}Why Is This So Important?
Think about it: You can use person information for car, house, and work information. Instead of writing id, name, age every time, you just use the PersonInfo class.
Prevents code repetition, becomes more organized, and makes changes easier. For example, if you want to add a phone number to the person, you only need to update the PersonInfo class.
Composition Example in Flutter
We actually use composition all the time when writing widgets in Flutter:
class UserCard extends StatelessWidget {
final User user; // Composition
UserCard({required this.user});
@override
Widget build(BuildContext context) {
return Card(
child: ListTile(
title: Text(user.name),
subtitle: Text(user.email),
leading: CircleAvatar(
child: Text(user.name[0]),
),
),
);
}
}As you can see, we're using the User class directly. This way, when user information changes, we only update in one place.
What Did I Learn Today?
- Enums organize code and reduce the chance of errors
- Composition allows us to combine classes (has-a relationship)
- Both are very commonly used structures in Flutter
Tomorrow I'll look at inheritance and polymorphism topics. Those look even more interesting!
Learning Resources
To understand Enum and Composition topics:
- Dart's official documentation
- My own experimental code
- Examples on StackOverflow
I hope this article was useful for you. Feel free to comment if you have questions!
Contact: