DEV Community

SOFTCODE9X
SOFTCODE9X

Posted on

How do I use generics in Dart to create a reusable data structure like a Stack<T>?

Generics in Dart allow you to create reusable and type-safe data structures and functions. By defining a class or method with a type parameter, you can work with different data types without sacrificing type safety. For example, a generic Stack class can store elements of any type, such as integers, strings, or custom objects, while ensuring that only the specified type is used throughout the stack's operations. This not only makes your code more flexible and reusable but also helps prevent runtime errors by catching type mismatches at compile time. Using generics, you can build robust and versatile components that integrate seamlessly into various parts of your application.
Here's an example implementation of a generic Stack in Dart:

class Stack {
final List _stack = [];

void push(T value) {
_stack.add(value);
}
T pop() {
if (_stack.isEmpty) {
throw StateError('No elements in the stack');
}
return _stack.removeLast();
}

T peek() {
if (_stack.isEmpty) {
throw StateError('No elements in the stack');
}
return _stack.last;
}

bool get isEmpty => _stack.isEmpty;
int get length => _stack.length;
}

Usage Example:
void main() {
Stack intStack = Stack();
intStack.push(1);
intStack.push(2);
print(intStack.pop()); // Outputs: 2

Stack stringStack = Stack();
stringStack.push('hello');
stringStack.push('world');
print(stringStack.peek()); // Outputs: 'world'
}

Explanation:
Generic Type : The Stack class is defined with a generic type parameter T, allowing it to store elements of any type.
Internal List: The _stack list holds the stack elements.
Stack Operations: Methods like push, pop, peek, and isEmpty provide standard stack functionality.
Type Safety: Using generics ensures that the stack is type-safe and can be used with any data type.

Hire Me: https://www.fiverr.com/s/GzyjzyZ

Behance Portfolio: https://www.behance.net/susmoydutta

Linkedin Profile: https://www.linkedin.com/in/susmoy-dutta/

FlutterApp #FlutterUi #Flutter #FlutterDeveloper #FlutterFirbase #Amanda #Paula #SusmoyDutta #infoosp #softcode9x #SoftCode9X #SoftCodeNinex #NineSwiftCode #SoftCodeQuick9 #9XSoftPro #NineSpeedCode #FastCode9X #NineXSoft #SwiftSoft9X #9XVelocityCode #TurboSoft9 #RapidCode9X #NineXAccel #SoftCodeRush9 #9XHyperCode #NineXExpress #SoftCode9Jet #QuickSoft9X #NineXBoost #SoftCode9Turbo #9XFlashCode #Innovation #Speed #Efficiency #Modern #Technology #Sleek #Dynamic #Precision #Digital #Simplicity #Futuristic #Connectivity #Code #Software #Minimalist #Agility #Performance #Scalability #Edge #Flow #Cutting-edge #Versatility #Innovation #Matrix #Circuit #Abstract #Cloud #Binary #Integration #Velocity #Quantum #Grid #Hex #Pixel #Network #Algorithm #Synergy #Revolution #Core #Nexus #bastSoftCode9X #techsoftCode9X #itSoftCode9X#dev #SoftCode9X #softwareSoftCode9X #infoSoftCode9X #tclSoftCode9X #flutterSoftCode9X #websitSoftCode9X #mobileSoftCode9X #softwaredevelpomentSoftCode9X #developSoftCode9X #softwareSusmoydutta #SoftwareDeveloperSusmoyDutta #infoosplife

Image description

Top comments (0)