Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lernen Enums and Extensions for Flutter Code | Dart Language Core for Flutter
Dart for Flutter Developers

bookEnums and Extensions for Flutter Code

Enums in Dart are a powerful tool for representing a fixed set of related values, such as the possible states of a widget in Flutter. By using enums, you can make your code more readable and less error-prone, especially when handling things like loading, success, or error states in your UI.

main.dart

main.dart

copy
123456789101112131415161718192021
import 'package:flutter/material.dart'; enum LoadState { loading, success, error } class StatusWidget extends StatelessWidget { final LoadState state; const StatusWidget({Key? key, required this.state}) : super(key: key); @override Widget build(BuildContext context) { switch (state) { case LoadState.loading: return const CircularProgressIndicator(); case LoadState.success: return const Icon(Icons.check, color: Colors.green); case LoadState.error: return const Icon(Icons.error, color: Colors.red); } } }

Dart extensions let you add custom methods or getters to existing types without modifying their original source code. This is especially useful for making your Flutter code cleaner and more expressive, such as by adding formatting helpers to strings or utility methods to collections.

main.dart

main.dart

copy
1234567891011
extension DisplayFormat on String { String toTitleCase() { if (isEmpty) return this; return this[0].toUpperCase() + substring(1).toLowerCase(); } } void main() { String name = "flutter"; print(name.toTitleCase()); // Output: Flutter }
Note
Note

Using enums instead of strings for state management helps prevent typos, makes refactoring safer, and allows for better code completion in your IDE.

question mark

Which statement best describes the benefit of using enums and extensions in Flutter code?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5

Fragen Sie AI

expand

Fragen Sie AI

ChatGPT

Fragen Sie alles oder probieren Sie eine der vorgeschlagenen Fragen, um unser Gespräch zu beginnen

bookEnums and Extensions for Flutter Code

Swipe um das Menü anzuzeigen

Enums in Dart are a powerful tool for representing a fixed set of related values, such as the possible states of a widget in Flutter. By using enums, you can make your code more readable and less error-prone, especially when handling things like loading, success, or error states in your UI.

main.dart

main.dart

copy
123456789101112131415161718192021
import 'package:flutter/material.dart'; enum LoadState { loading, success, error } class StatusWidget extends StatelessWidget { final LoadState state; const StatusWidget({Key? key, required this.state}) : super(key: key); @override Widget build(BuildContext context) { switch (state) { case LoadState.loading: return const CircularProgressIndicator(); case LoadState.success: return const Icon(Icons.check, color: Colors.green); case LoadState.error: return const Icon(Icons.error, color: Colors.red); } } }

Dart extensions let you add custom methods or getters to existing types without modifying their original source code. This is especially useful for making your Flutter code cleaner and more expressive, such as by adding formatting helpers to strings or utility methods to collections.

main.dart

main.dart

copy
1234567891011
extension DisplayFormat on String { String toTitleCase() { if (isEmpty) return this; return this[0].toUpperCase() + substring(1).toLowerCase(); } } void main() { String name = "flutter"; print(name.toTitleCase()); // Output: Flutter }
Note
Note

Using enums instead of strings for state management helps prevent typos, makes refactoring safer, and allows for better code completion in your IDE.

question mark

Which statement best describes the benefit of using enums and extensions in Flutter code?

Select the correct answer

War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 1. Kapitel 5
some-alt