Flutter & Dart: Enumeration Example
This article will show you how to use enums (also known as enums or enum types) in Dart and Flutter.
An enumeration in Dart is a set of symbolic names (members) bound to a unique constant value. In an enumeration, members can be compared by identity, and the enumeration itself can be iterated over.
An enumeration can be declared using the enum keyword:
enum Aniaml {dog, cat, chicken, dragon}
Each value of the enumeration has an index. The index of the first value is 0.
You can retrieve all values of an enumeration using the values constant:
print(Aniaml.values);
// [Aniaml.dog, Aniaml.cat, Aniaml.chicken, Aniaml.dragon]
a complete example
coding:
// 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!
Flutter & Dart: Check if a date is between two other dates
To check if a date is between two other dates in Flutter and Dart, you can use the isBefore( ) and isAfter() methods of the DateTime class .
example
import 'package:flutter/foundation.dart';
DateTime dateA = DateTime(1900, 9, 14);
DateTime dateB = DateTime(2000, 10, 15);
DateTime dateC = DateTime(1984, 12, 20);
DateTime dateD = DateTime.now();
void main() {
if (dateA.isBefore(dateC) && dateB.isAfter(dateC)) {
debugPrint("dateC is between dateA and dateB");
} else {
debugPrint("dateC isn't between dateA and dateC");
}
if (dateA.isBefore(dateD) && dateB.isAfter(dateD)) {
debugPrint("dateD is between dateA and dateB");
} else {
debugPrint("dateD isn't between dateA and dateC");
}
}
output:
dateC is between dateA and dateB
dateD isn't between dateA and dateC
find more information about DateTime class in the official documentation.
Read More:
- Flutter & Dart every() Method Example
- Flutter & Dart Regular Expression Examples
- torch.max Example (How to Use)
- Python: How to Create List by Comprehension (Example Codes)
- MySQL Batch Add Data and Store Example
- File class details (get the file name, size, path, create, etc.)
- Base64 Image Compression Example
- MAFIA: 1- OpenFlow statistics (Counters, Timestamps)(mafia-sdn/p4demos/demos/1-openflow/1.1-statistics/p4src/of.p4)
- Opentelemetry + Jaeger Python Version Cross Service Call Example
- Hutool Excel Import & Export Example
- Android: How to Add Background Music for Activity with Service
- Electron: How to Use BrowserWindow to Create a Window
- WCNSS_qcom_cfg.ini WIFI Configuration File Guide
- go sync.Mutex Lock Examples
- How to Use Printf in HAL Library
- Windows Core Audio APIs: How to Progress Loopback Recording and Generate WAV File
- C#: How to Get details of the directory where the currently running program is located
- Docker: How to build a rabbitmq image cluster
- MultipartFile Upload an Image Example
- Open CASCADE Technology 7.7.0 released