|
| 1 | +// Copyright (c) 2020, Google Inc. Please see the AUTHORS file for details. |
| 2 | +// All rights reserved. Use of this source code is governed by a BSD-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:built_collection/built_collection.dart'; |
| 6 | + |
| 7 | +void main() { |
| 8 | + // `built_collection` provides immutable equivalents of Dart SDK `List`, |
| 9 | + // `Set` and `Map`: `BuiltList`, `BuiltSet` and `BuiltMap`. |
| 10 | + |
| 11 | + // The easiest way to create them is from collection literals using `build`: |
| 12 | + var builtList = [1, 2, 3].build(); |
| 13 | + var builtSet = {1, 2, 3}.build(); |
| 14 | + var builtMap = {1: 'one', 2: 'two', 3: 'three'}.build(); |
| 15 | + |
| 16 | + // `BuiltList` and `BuiltSet` can also be constructed from any `Iterable`. |
| 17 | + // The unnamed constructors act like the SDK `from` constructors, meaning |
| 18 | + // the elements are type checked at runtime. |
| 19 | + builtList = BuiltList([1, 2, 3]); |
| 20 | + builtSet = BuiltSet([1, 2, 3]); |
| 21 | + |
| 22 | + // Alternatively, the `of` constructors match the type to the `Iterable` you |
| 23 | + // pass, and so do not need to check the type of elements. |
| 24 | + builtList = BuiltList.of([1, 2, 3]); |
| 25 | + builtSet = BuiltSet.of([1, 2, 3]); |
| 26 | + |
| 27 | + // `BuiltMap` can be constructed from a `Map` or a `BuiltMap`. |
| 28 | + builtMap = BuiltMap({1: 'one', 2: 'two', 3: 'three'}); |
| 29 | + |
| 30 | + // Immutable collections can't be updated, but you can create new instances |
| 31 | + // based on existing ones. The most convenient way to do that is the |
| 32 | + // `rebuild` methods, which give you access to each collection type's |
| 33 | + // corresponding builder type. |
| 34 | + |
| 35 | + // For example, to add some elements then sort: |
| 36 | + builtList = builtList.rebuild((b) => b |
| 37 | + ..addAll([7, 6, 5]) |
| 38 | + ..sort()); |
| 39 | + |
| 40 | + // Generally, built collections match the SDK collections, except that the |
| 41 | + // API has been split in two: read only methods go on the `Built` collection |
| 42 | + // types, and mutating methods go on the corresponding `Builder` types. |
| 43 | + |
| 44 | + // If you need to keep a mutable version of the collection around for a |
| 45 | + // while, for example to pass it to other methods, you can use `toBuilder`. |
| 46 | + // Then, later, the collection is made immutable again by calling `build`. |
| 47 | + var listBuilder = builtList.toBuilder(); |
| 48 | + listBuilder.addAll([10, 9, 8]); |
| 49 | + // More changes could go here, including passing the builder to other |
| 50 | + // methods. |
| 51 | + builtList = listBuilder.build(); |
| 52 | + |
| 53 | + // Finally, `built_collection` also provides immutable versions of |
| 54 | + // `ListMultimap` and `SetMultimap` from `package:quiver`. For information |
| 55 | + // on these, and full details on all the APIs, please see the package |
| 56 | + // [dartdoc](https://pub.dev/documentation/built_collection/latest). |
| 57 | +} |
0 commit comments