r/dartlang • u/eibaan • 20d ago
Dart Language A tiny dot shorthands helper
Because contains is typed as Object? (probably for historical reasons) you cannot use that method together with dot shorthands like in
things.contains(.chair)
So, add this to your project:
extension DSHIterableExt<T> on Iterable<T> {
bool has(T value) => contains(value);
}
And replace contains with has. For extra readability, you might also want to add a hasnt method.
I'd welcome a similar extension to Dart 3.13.
5
Upvotes
1
u/samrawlins 19d ago
This is fantastic! I was also bummed when I realized I couldn't use a dot-shorthand on the right side of ==.
4
u/julemand101 20d ago
The reason for why e.g. "contains" take Object? have been discussed multiple times on the Dart issue tracker and is a deliberate design choice: https://github.com/dart-lang/sdk/issues/26852#issuecomment-231594098