Updated 20 September 2024
Enumeration in Flutter provides a way to define a set of named values, which can make your code more readable and maintainable. In this article, we’ll dive into how to use enums in Flutter, explore their benefits, and uncover some advanced use cases.
In Dart, an enum is a special type of class that represents a collection of constant values.
Enums are particularly useful when dealing with scenarios where a variable should only be able to take one value out of a small set of possibilities. Enums are useful when you need to work with a fixed set of related values, such as days of the week or the months of the year.
To define an enum in Dart, use the enum
keyword followed by the name of the enum and its possible values. Some examples are below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
//1. enum Weekdays { monday, tuesday, wednesday, thursday, friday, saturday, sunday, } //2. enum Statuses { loading, success, error } |
Once you declare an enum, you can access its values by referencing them using the dot (.
) notation.
To use an enum in Flutter, you can simply assign the constants to a variable of the Statuses type, like:
1 |
Weekdays dayOfTheWeek = Weekdays.monday;<br>Statuses currentStatus = Statuses.loading;<br><br><br>void checkStatuses(Statuses statuses) {<br> if (statuses == Statuses.loading) {<br> print("App is loading...");<br> } else if (statuses == Statuses.success) {<br> print("Success!");<br> } else if (statuses == Statuses.error) {<br> print("There is an error occurred.");<br> }<br>}<br> |
Furthermore, in the above example, we pass the enum value to a function and check it using conditionals.
Dart provides several useful methods that you can use with enums:
The “index” property gives you the position of the enum value, starting from 0
.
1 |
void main() {<br> print(Statuses.loading.index); // Output: 0<br> print(Statuses.success.index); // Output: 1<br> print(Statuses.error.index); // Output: 2<br>} |
The “values” property provides a list of all enum values in the order they are defined.
1 |
void main() {<br> for (var statuses in Statuses.values) {<br> print(statuses);<br> }<br>}<br><br>// Output:<br>// Statuses.loading<br>// Statuses.success<br>// Statuses.error |
In your app, you may want to handle different network request states, such as loading, success, and error. You can use an enum to represent these states.
Here is an example of this.
1 |
class NetworkHandler extends StatelessWidget {<br> final Statuses state;<br><br> NetworkHandler(this.state);<br><br> @override<br> Widget build(BuildContext context) {<br> switch (state) {<br> case Statuses.loading:<br> return CircularProgressIndicator();<br> case Statuses.success:<br> return Text('Data loaded successfully!');<br> case Statuses.error:<br> return Text('Failed to load data.');<br> }<br> }<br>} |
Enumeration in Flutter offers a clear and maintainable way to handle constant values, states, and conditions. They enhance code safety by restricting the possible values a variable can hold, making your logic more intuitive and reliable.
Hope you enjoyed this article.
Please check my other blogs here.
You can check the official documentation here.
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.