パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v6.0.0

built_collection

SDKのコレクションに基づく不変コレクション。各SDKコレクションクラスは、新しい不変コレクションクラスと対応する変更可能なビルダークラスに分割されています。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
1240万
いいね数
347
Pub ポイント
150

パッケージをインストール

flutter pub add built_collection

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

Built Collection は、Dart の標準コレクションの不変バージョンを提供し、意図しない変更を防ぐことでデータの整合性を保証します。List、Map、Set などの各可変 SDK コレクションには、対応する不変バージョンと、インスタンスを構築するための可変ビルダーがあります。これにより関数型プログラミングの実践が促進され、アプリケーションにおけるより安全な状態管理が可能になります。リアクティブまたは状態を持つ 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.

カテゴリー

ソースリポジトリ

このパッケージを使用しているプロジェクト

ランキング