Flutter Smart Generics π‘
A powerful, open-source Flutter package offering ready-to-use generic functions, data utilities, and stateless helpers to accelerate development, reduce boilerplate, and boost app maintainability.
β¨ Why Smart Generics?
- β Eliminate repetitive boilerplate (e.g. parsing, casting, list rendering)
- π§ Improve type safety with reusable patterns
- π Build apps faster with fewer lines of code
- π§ͺ Easier testing with consistent logic structures
- π Open source and ready for community contribution
π¦ Package Structure
lib/
βββ core/
β βββ parser.dart # Generic JSON parsing
β βββ list_builder.dart # Type-safe list rendering widget
β βββ type_utils.dart # Safe casting helpers
β βββ result.dart # Result<T> pattern for success/failure
β βββ comparator.dart # Generic sorting helpers
β βββ debounce_throttle.dart # Debouncer and Throttler utilities
example/
βββ main.dart # Usage examples
π Getting Started
Installation
Add to your pubspec.yaml:
dependencies:
flutter_smart_generics: ^1.0.0
Then run:
flutter pub get
π§ Features
1. π Generic JSON Parser
typedef FromJson<T> = T Function(Map<String, dynamic>);
T parseData<T>(Map<String, dynamic> json, FromJson<T> converter) => converter(json);
Use:
final user = parseData<User>(json, User.fromJson);
2. π§± Reusable List Widget (with builder)
Widget buildList<T>({
required List<T> items,
required Widget Function(T item) builder,
Axis scrollDirection = Axis.vertical,
bool shrinkWrap = false,
ScrollPhysics? physics,
}) {
return ListView.builder(
itemCount: items.length,
itemBuilder: (context, index) => builder(items[index]),
scrollDirection: scrollDirection,
shrinkWrap: shrinkWrap,
physics: physics,
);
}
Use:
buildList<User>(items: users, builder: (u) => ListTile(title: Text(u.name)));
3. π§ͺ Safe Type Casting
T? tryCast<T>(dynamic value) => value is T ? value : null;
bool isNotNull<T>(T? value) => value != null;
Use:
final age = tryCast<int>(json['age']);
4. β Functional Result Pattern
sealed class Result<T> {
const Result();
factory Result.success(T data) = Success;
factory Result.failure(String message) = Failure;
}
class Success<T> extends Result<T> {
final T data;
const Success(this.data);
}
class Failure<T> extends Result<T> {
final String message;
const Failure(this.message);
}
Use:
Future<Result<User>> fetchUser() async {
try {
final json = await api.get();
return Result.success(parseData<User>(json, User.fromJson));
} catch (e) {
return Result.failure(e.toString());
}
}
5. π Generic Comparator
List<T> sortBy<T>(List<T> items, Comparable Function(T) keySelector, {bool descending = false}) {
final sorted = [...items];
sorted.sort((a, b) => descending
? keySelector(b).compareTo(keySelector(a))
: keySelector(a).compareTo(keySelector(b)));
return sorted;
}
Use:
final sorted = sortBy<User>(users, (u) => u.name);
6. β±οΈ Debouncer and Throttler
final debouncer = Debouncer<String>(Duration(milliseconds: 300));
debouncer.action = (searchTerm) async {
final results = await fakeApiSearch(searchTerm);
print('Results for "$searchTerm": $results');
};
final throttler = Throttler<String>(Duration(seconds: 1));
throttler.action = (value) => print('Clicked: $value');
π² Example
Check example/main.dart to see how to integrate the utilities into a working Flutter app.
π§ͺ Testing
Run all tests with:
flutter test
π€ Contributing
Pull requests are welcome! Please write tests and follow Conventional Commits.
π License
MIT License Β© 2025 Rafael Arango PΓ©rez β see LICENSE.
Built with β€οΈ to help Flutter devs ship faster and smarter.