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.