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

套件詳情v3.0.3+1

sealed_unions

Dart 的封閉聯合類型

AndroidiOSWebmacOSWindowsLinux
近 30 天下載
44
喜歡數
47
Pub 分數
120

安裝套件

flutter pub add sealed_unions

套件介紹

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

Sealed Unions for Dart 是一個套件,提供了一種類型安全的方式來在 Dart 中建模總類型(也稱為代數資料類型)。它讓開發人員能夠定義類型的有限可能情況集合,確保在編譯時進行全面的模式匹配。這有助於防止執行時錯誤,並在處理多個不同的狀態或結果時提升程式碼清晰度。該套件在 Flutter 和 Dart 應用程式中的函數式程式設計模式中特別有用。

適用情境

  • 處理UI中的狀態轉換
  • 使用多種成功/失敗變體對 API 回應進行建模
  • 實現有限狀態機
  • 使用類型化結果管理錯誤處理
  • 使用清晰的案例區分構建領域模型

主要特色

  • 聯合情況的編譯時安全性
  • 全面的模式匹配支援
  • 定義密封類型時的簡潔語法
  • 與 Dart 的空安全整合
  • 支援嵌套和遞迴聯合

適合對象

  • 使用函數式程式設計模式的開發人員
  • 需要強大狀態管理的Flutter應用
  • 重視類型安全和可維護性的團隊
  • 具有複雜條件邏輯的項目
  • 需要清晰錯誤和結果處理的應用程式

使用建議

  • 需要理解求和類型和模式匹配
  • 可能增加新團隊成員的初期學習曲線
  • 不適合沒有變體邏輯的簡單資料結構
  • 受限於 Dart 的靜態分析約束
  • 最好與lint規則搭配使用,以獲得最佳安全性

原始 README

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

Flutter Community: sealed_unions

ACKNOWLEDGEMENTS

This library is a port of JavaSealedUnions originally created by Francisco (Paco) Estévez .

Dart port by George Medve with assistance from Jan Knotek and Brian Egan.

RATIONALE

Sealed classes are used for representing restricted class hierarchies, when a value can have one of the types from a limited set, but cannot have any other type. They are, in a sense, an extension of enum classes: the set of values for an enum type is also restricted, but each enum constant exists only as a single instance, whereas a subclass of a sealed class can have multiple instances which can contain state. Sealed classes are available in languages like Kotlin, or C#. Tagged unions are common on Swift and Rust.

We highly recommend reading the Rationale for the readme of JavaSealedUnions for an indepth explanation of the idea and application of the library.

MOTIVATION

This library was heavily inspired by RxEither and the wonderful Domain Driven Design (DDD) talk by Scott Wlaschin. This video by Scott Wlaschin will inspire you on how to apply DDD: Domain Driven Design, F# and Types

USAGE

The following example project: MVI Sealed Unions demonstrates the library application in a Flutter app.

INSTALLING

1. Depend on it

Add this to your package's pubspec.yaml file:

dependencies:
  sealed_unions: "^3.0.3+1"
2.Install it

You can install packages from the command line:

with Flutter:

flutter packages get

with pub:

pub get
3. Import it

Now in your Dart code, you can use:

// for example import Union4
import 'package:sealed_unions/union_4.dart';

License

Copyright (c) fluttercommunity 2019

The Apache Software License, Version 2.0

See LICENSE.md

Flutter Community: sealed_unions

所屬分類

原始碼儲存庫

使用此套件的專案