Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pkgs/collection/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 1.20.0-wip

- `CanonicalizedMap.cast` returns a view of the public keys and values.
- Adds `separated` and `separatedList` extension methods to `Iterable`.
- Adds `separate` extension method to `List`
- Add `IterableMapEntryExtension` for working on `Map` as a list of pairs, using
Expand Down
6 changes: 5 additions & 1 deletion pkgs/collection/lib/src/canonicalized_map.dart
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ class CanonicalizedMap<C, K, V> implements Map<K, V> {
);

@override
Map<K2, V2> cast<K2, V2>() => _base.cast<K2, V2>();
Map<K2, V2> cast<K2, V2>() {
// `_base` stores canonical keys and `MapEntry`s, not the public `K`/`V`
// view. Casting that storage makes lookups fail with a cast error.
return Map.castFrom<K, V, K2, V2>(this);
}

@override
void clear() {
Expand Down
8 changes: 8 additions & 0 deletions pkgs/collection/test/canonicalized_map_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,14 @@ void main() {
test('cast returns a new map instance', () {
expect(map.cast<Pattern, Pattern>(), isNot(same(map)));
});

test('cast preserves lookups on the public key and value types', () {
map.addAll({'01': 'value 01', '2': 'value 2'});
final casted = map.cast<String, String>();
expect(casted['01'], 'value 01');
expect(casted['1'], 'value 01');
expect(casted.length, 2);
});
});

group('CanonicalizedMap builds an informative string representation', () {
Expand Down
Loading