diff options
Diffstat (limited to 'lib/first_where_ext.dart')
-rw-r--r-- | lib/first_where_ext.dart | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/lib/first_where_ext.dart b/lib/first_where_ext.dart new file mode 100644 index 0000000..7e297ff --- /dev/null +++ b/lib/first_where_ext.dart @@ -0,0 +1,9 @@ +extension FirstWhereExt<T> on List<T> { + /// The first element satisfying [test], or `null` if there are none. + T? firstWhereOrNull(bool Function(T element) test) { + for (final element in this) { + if (test(element)) return element; + } + return null; + } +} |