Built Collection은 Dart 표준 컬렉션의 불변 버전을 제공하여 의도하지 않은 수정을 방지함으로써 데이터 무결성을 보장합니다. 각 가변 SDK 컬렉션(List, Map, Set 등)에는 해당하는 불변 변형과 인스턴스를 생성하기 위한 가변 빌더가 있습니다. 이는 함수형 프로그래밍 관행을 촉진하고 애플리케이션에서 더 안전한 상태 관리를 가능하게 합니다. 반응형 또는 상태 기반 UI 프레임워크에서는 불변성이 예측 가능성과 디버깅을 향상시키므로 이 패키지는 특히 유용합니다. Flutter 및 기타 Dart 기반 플랫폼과 원활하게 통합됩니다.
사용 사례
불변 상태 관리
함수형 프로그래밍 패턴
컴포넌트 간의 안전한 데이터 공유
반응형 UI 개발
의도하지 않은 변경 방지
앱에서의 상태 지속성
주요 기능
불변 컬렉션 클래스
가변적 빌더 패턴
타입 안전한 생성
Dart SDK와의 원활한 통합
모든 주요 플랫폼 지원
클린한 API 설계
적합한 대상
불변성을 찾는 플러터 개발자
예측 가능한 상태 변경이 필요한 앱
함수형 프로그래밍을 채택하는 팀
복잡한 상태 로직을 가진 대규모 애플리케이션
반응형 상태 관리 사용 개발자
고려 사항
불변성으로 인한 약간의 성능 오버헤드
빌더 패턴의 학습 곡선
고주파 변이 시나리오에 적합하지 않습니다
컬렉션 업데이트를 주의 깊게 다뤄야 합니다
복사로 인해 메모리 사용량이 증가할 수 있습니다
Provider 또는 Bloc과 같은 상태 관리 솔루션과 가장 잘 어울립니다
원본 README
연결된 소스 저장소의 README 원문 스냅샷입니다. 최신 내용은 저장소에서 확인하세요.
Introduction
Built Collections are immutable collections using the
builder pattern.
Each of the core SDK collections is split in two: a mutable builder class
and an immutable "built" class. Builders are for computation,
"built" classes are for safely sharing with no need to copy defensively.
Immutable collections work particularly well with immutable values. See
built_value.
You can read more about built_collection
on medium.
Design
Built Collections:
are immutable, if the elements/keys/values used are immutable;
are comparable;
are hashable;
use copy-on-write to avoid copying unnecessarily.
See below for details on each of these points.
Recommended Style
A project can benefit greatly from using Built Collections throughout.
Methods that will not mutate a collection can accept the "built" version,
making it clear that no mutation will happen and completely avoiding
the need for defensive copying.
For code that is public to other projects or teams not using
Built Collections, prefer to accept Iterable where possible. That way
your code is compatible with SDK collections, Built Collections and any
other collection implementation that builds on Iterable.
It's okay to accept List, Set or Map if needed. Built Collections
provide efficient conversion to their SDK counterparts via
BuiltList.toList, BuiltListMultimap.toMap, BuiltSet.toSet,
BuiltMap.toMap and BuiltSetMultimap.toMap.
Built Collections are Immutable
Built Collections do not offer any methods that modify the collection. In
order to make changes, first call toBuilder to get a mutable builder.
In particular, Built Collections do not implement or extend their mutable
counterparts. BuiltList implements Iterable, but not List. BuiltSet
implements Iterable, but not Set. BuiltMap, BuiltListMultimap and
BuiltSetMultimap share no interface with the SDK collections.
Built Collections can contain mutable elements. However, this use is not
recommended, as mutations to the elements will break comparison and
hashing.
Built Collections are Comparable
Core SDK collections do not offer equality checks by default.
Built Collections do a deep comparison against other Built Collections
of the same type using Dart's equality operator (==). A Built Collection
cannot be compared to another Built Collection when they are of different
types, such as a BuiltList and a BuiltSet. Hashing is used to make
repeated comparisons fast.
// true: same contents according to `int.operator==`, `MyClass.operator==`BuiltList([1, 2, 3]) == BuiltList([1, 2, 3]);
BuiltList([MyClass(1), MyClass(2)]) == BuiltList([MyClass(1), MyClass(2)]);
// false: BuiltList and BuiltSet are never equalBuiltList([1, 2, 3]) == BuiltSet([1, 2, 3]);
// false: different contents according to `int.operator==`BuiltList([1, 2, 3]) == BuiltList([2, 3, 4]);
Built Collections are Hashable
Core SDK collections do not compute a deep hashCode.
Built Collections do compute, and cache, a deep hashCode. That means they
can be stored inside collections that need hashing, such as hash sets and
hash maps. They also use the cached hash code to speed up repeated
comparisons.
Built Collections Avoid Copying Unnecessarily
Built Collections and their builder and helper types collaborate to avoid
copying unless it's necessary.
In particular, BuiltList.toList, BuiltListMultimap.toMap,
BuiltSet.toSet, BuiltMap.toMap and BuiltSetMultimap.toMap do not make
a copy, but return a copy-on-write wrapper. So, Built Collections can be
efficiently and easily used with code that needs core SDK collections but
does not mutate them.
When you want to provide a collection that explicitly throws when a
mutation is attempted, use BuiltList.asList,
BuiltListMultimap.asMap, BuiltSet.asSet, BuiltSetMultimap.asMap
and BuiltMap.asMap.
Features and bugs
Please file feature requests and bugs at the issue tracker.