How to Define a Generic Class in Dart
A generic class in Dart allows you to create a class that can operate on different data types while maintaining type safety. By using type parameters, you can define a class that can work with any type of data, making your code more flexible and reusable.
1. Defining a Generic Class
To define a generic class, you specify a type parameter in angle brackets (<T>
) after the class name. This type parameter can then be used within the class to define properties and methods.
Example of a Generic Class
class Box<t> {
T item;
Box(this.item); // Constructor to initialize the item
T getItem() {
return item; // Method to return the item
}
}
</t>
In this example, we define a generic class Box
with a type parameter T
. The class has a property item
of type T
, a constructor to initialize the item, and a method getItem
that returns the item.
2. Creating Instances of a Generic Class
When you create an instance of a generic class, you specify the type you want to use in place of the type parameter. This allows you to create instances of the class for different data types.
Example of Creating Instances
void main() {
// Creating a Box for integers
Box<int> intBox = Box<int>(10);
print(intBox.getItem()); // Output: 10
// Creating a Box for strings
Box<string> stringBox = Box<string>('Hello, Dart!');
print(stringBox.getItem()); // Output: Hello, Dart!
}
</string></string></int></int>
In this example, we create two instances of the Box
class: one for integers and one for strings. We specify the type when creating the instance, allowing the class to work with different data types while maintaining type safety.
3. Benefits of Using Generic Classes
- Type Safety: Generics provide compile-time type checking, which helps catch type-related errors early in the development process.
- Code Reusability: You can write a single implementation that works with different data types, reducing code duplication.
- Improved Readability: Generics make it clear what type of data a class is working with, improving code clarity.
4. Bounded Generics
Dart also supports bounded generics, which allow you to restrict the types that can be used as type parameters. You can specify a superclass or interface that the type parameter must extend or implement.
Example of Bounded Generics
class ComparableBox<t extends comparable> {
T item;
ComparableBox(this.item);
bool isGreaterThan(T other) {
return item.compareTo(other) > 0;
}
}
</t>
In this example, we define a generic class ComparableBox
that restricts the type parameter T
to types that implement the Comparable
interface. This allows us to use the compareTo
method to compare items.
5. Conclusion
Defining a generic class in Dart is a straightforward process that enhances the flexibility and reusability of your code. By using type parameters, you can create classes that work with any data type while maintaining type safety. Understanding how to define and use generic classes effectively is essential for building robust and maintainable Dart applications.