Search packages, repositories and app categories

Package detailsv3.0.3+1

sealed_unions

Sealed Unions for Dart

AndroidiOSWebmacOSWindowsLinux
30-day downloads
44
Likes
47
Pub points
120

Install package

flutter pub add sealed_unions

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

Sealed Unions for Dart is a package that provides a type-safe way to model sum types (also known as algebraic data types) in Dart. It enables developers to define a finite set of possible cases for a type, ensuring exhaustive pattern matching at compile time. This helps prevent runtime errors and improves code clarity when handling multiple distinct states or outcomes. The package is particularly useful in functional programming patterns within Flutter and Dart applications.

Use cases

  • Handling state transitions in UI
  • Modeling API responses with multiple success/failure variants
  • Implementing finite state machines
  • Managing error handling with typed outcomes
  • Building domain models with clear case distinctions

Key features

  • Compile-time safety for union cases
  • Exhaustive pattern matching support
  • Clean syntax for defining sealed types
  • Integration with Dart's null safety
  • Support for nested and recursive unions

Suitable for

  • Developers using functional programming patterns
  • Flutter apps requiring robust state management
  • Teams prioritizing type safety and maintainability
  • Projects with complex conditional logic
  • Applications needing clear error and result handling

Considerations

  • Requires understanding of sum types and pattern matching
  • May increase initial learning curve for new team members
  • Not suitable for simple data structures without variant logic
  • Limited to Dart's static analysis constraints
  • Best used in conjunction with linter rules for optimal safety

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

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

Categories

Source repository

Projects using this package