搜索插件、代码仓与 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.

所属分类

源码仓库

使用此插件的项目

所在榜单