Use Generics & Extension Methods to express stable APIs and exhaustive domain models. Prefer composition, sealed hierarchies, constrained generics, and focused extensions while minimizing hidden mutation and unchecked casts.
Build a Complete Mental Model
Use Generics & Extension Methods to express stable APIs and exhaustive domain models. Prefer composition, sealed hierarchies, constrained generics, and focused extensions while minimizing hidden mutation and unchecked casts.
Apply It in Modern Dart
Run and test an original Generics & Extension Methods example with null, empty, boundary, invalid, async-error, and cancellation cases. Use dart format, dart analyze, and dart test, and verify Future or Stream subscriptions and isolate boundaries where relevant.
After This Lesson
T? firstOrNull<T>(List<T> values) => values.isEmpty ? null : values.first;
extension IntTotals on Iterable<int> {
int get total => fold(0, (sum, value) => sum + value);
}
void main() {
print(firstOrNull(<String>['Dart']));
print([1, 2, 3].total);
}Dart
6Try It Yourself
Run and test an original Generics & Extension Methods example with null, empty, boundary, invalid, async-error, and cancellation cases. Use dart format, dart analyze, and dart test, and verify Future or Stream subscriptions and isolate boundaries where relevant.
Null and Async Warning
Assuming one sample output proves every null, promotion, Future, Stream, and isolate path.
Dart language overview — Dart