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

所属分类

源码仓库

使用此插件的项目