diff --git a/sdk/lib/collection/collections.dart b/sdk/lib/collection/collections.dart index 9b43996f6381..1137f526b9be 100644 --- a/sdk/lib/collection/collections.dart +++ b/sdk/lib/collection/collections.dart @@ -8,6 +8,17 @@ part of dart.collection; /// /// The source of the elements may be a [List] or any [Iterable] with /// efficient [Iterable.length] and [Iterable.elementAt]. +/// +/// ```dart +/// final numbers = [10, 20, 30]; +/// final unmodifiableListView = UnmodifiableListView(numbers); +/// +/// // Insert new elements into the original list. +/// numbers.addAll([40, 50]); +/// print(unmodifiableListView); // [10, 20, 30, 40, 50] +/// +/// unmodifiableListView.remove(20); // Throws. +/// ``` class UnmodifiableListView extends UnmodifiableListBase { final Iterable _source; diff --git a/sdk/lib/collection/maps.dart b/sdk/lib/collection/maps.dart index b0ab2708db0c..b8673b15ed0d 100644 --- a/sdk/lib/collection/maps.dart +++ b/sdk/lib/collection/maps.dart @@ -200,7 +200,7 @@ abstract class MapMixin implements Map { /// Basic implementation of an unmodifiable [Map]. /// /// This class has a basic implementation of all but two of the members of -/// an umodifiable [Map]. +/// an unmodifiable [Map]. /// A simple unmodifiable `Map` class can be implemented by extending this /// class and implementing `keys` and `operator[]`. /// @@ -376,6 +376,17 @@ class MapView implements Map { /// A wrapper around a `Map` that forwards all members to the map provided in /// the constructor, except for operations that modify the map. /// Modifying operations throw instead. +/// +/// ```dart +/// final baseMap = {1: 'Mars', 2: 'Mercury', 3: 'Venus'}; +/// final unmodifiableMapView = UnmodifiableMapView(baseMap); +/// +/// // Remove an entry from the original map. +/// baseMap.remove(3); +/// print(unmodifiableMapView); // {1: Mars, 2: Mercury} +/// +/// unmodifiableMapView.remove(1); // Throws. +/// ``` class UnmodifiableMapView extends MapView with _UnmodifiableMapMixin { UnmodifiableMapView(Map map) : super(map); diff --git a/sdk/lib/collection/set.dart b/sdk/lib/collection/set.dart index 81ca0a8c259a..f59b56d30712 100644 --- a/sdk/lib/collection/set.dart +++ b/sdk/lib/collection/set.dart @@ -400,6 +400,17 @@ class _UnmodifiableSet extends _SetBase with _UnmodifiableSetMixin { /// /// Methods that could change the set, such as [add] and [remove], /// must not be called. +/// +/// ```dart +/// final baseSet = {'Mars', 'Mercury', 'Earth', 'Venus'}; +/// final unmodifiableSetView = UnmodifiableSetView(baseSet); +/// +/// // Remove an element from the original set. +/// baseSet.remove('Venus'); +/// print(unmodifiableSetView); // {Mars, Mercury, Earth} +/// +/// unmodifiableSetView.remove('Earth'); // Throws. +/// ``` @Since("2.12") class UnmodifiableSetView extends SetBase with _UnmodifiableSetMixin { final Set _source;