搜尋套件、儲存庫與 APP 分類

套件詳情v6.0.0

built_collection

基於 SDK 集合的不可變集合。每個 SDK 集合類別都拆分為新的不可變集合類別與對應的可變建構器類別。

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
1240萬
喜歡數
347
Pub 分數
150

安裝套件

flutter pub add built_collection

套件介紹

根據套件公開資料產生的選型評估,僅供參考。

Built Collection 提供 Dart 標準集合的不可變版本,透過防止意外修改來確保資料完整性。每個可變 SDK 集合(如 List、Map、Set)都有對應的不可變變體以及用於建構實例的可變建構器。這促進了函數式程式設計實踐,並在應用程式中實現更安全的狀態管理。該套件在反應式或有狀態 UI 架構中特別有用,其中不可變性可提升預測性和除錯能力。它與 Flutter 及其他基於 Dart 的平台無縫整合。

適用情境

  • 不可變狀態管理
  • 函數式程式設計模式
  • 跨元件的安全資料共用
  • 反應式UI開發
  • 防止意外修改
  • 應用中的狀態持久化

主要特色

  • 不可變集合類
  • 可變建構器模式
  • 類型安全的建構
  • 與 Dart SDK 的無縫整合
  • 支援所有主要平台
  • 乾淨的 API 設計

適合對象

  • 尋求不可變性的Flutter開發者
  • 需要可預測狀態變更的應用程式
  • 採用函數式程式設計的團隊
  • 具有複雜狀態邏輯的大規模應用
  • 使用響應式狀態管理的開發者

使用建議

  • 不可變性帶來的輕微性能開銷
  • 建構者模式的學習曲線
  • 不適合高頻率變異場景
  • 需要仔細處理集合更新
  • 由於複製可能導致記憶體使用量增加
  • 最佳搭配狀態管理解決方案,如 Provider 或 Bloc

原始 README

以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。

Build pub package package publisher

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 equal
BuiltList([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.

所屬分類

原始碼儲存庫

使用此套件的專案

所在榜單