union_type 1.0.1

  • README.md
  • Installing
  • Versions
  • 74


version 1.0.0+1 build status

Dart union type library. This library no longer depends on dart:mirrors.


union_type provides helpers for type assertions across types that do not share ancestry.

import 'package:union_type/union_type.dart';
const UnionType BAZ = const UnionType('Baz', types: const [Foo, Bar]);

main() {
    BAZ.enforce(new Foo()); // No error
    BAZ.enforce(new Bar()); // No error
    BAZ.enforce({}); // TypeError
    BAZ.enforceAll([1, 2]); // TypeError
    if (BAZ.check(foo)) {
        // Do something
    } else {
        // Do something else

Union types can assert other union types as well.

const UnionType MASTER =
    const UnionType('MasterType', types: const [BAZ, OtherType]);

You can also create a list of items, asserting that they match the type.

import 'package:union_type/union_type.dart';
const UnionType MONSTER =
    const UnionType('Monster', types: const [Franken, Stein]);

main() {
    var monsterArmy = new TypedList(MONSTER);

    monsterArmy.add(new Franken());
    monsterArmy.add(new Stein());

    for (var monster in monsterArmy) {
        print('MWAHAHAHA: $monster');

    monsterArmy.add(2); // TypeError
    monsterArmy.addAll([2, 3, 4, '5']); // TypeError

1. Depend on it

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

  union_type: "^1.0.1"

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter packages get

Alternatively, your editor might support pub get or packages get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:

import 'package:union_type/union_type.dart';
Version Uploaded Documentation Archive
1.0.1 Jul 15, 2017 Go to the documentation of union_type 1.0.1 Download union_type 1.0.1 archive
1.0.0+1 Dec 17, 2016 Go to the documentation of union_type 1.0.0+1 Download union_type 1.0.0+1 archive
1.0.0 Dec 17, 2016 Go to the documentation of union_type 1.0.0 Download union_type 1.0.0 archive


This feature is new.
We welcome feedback.
More details: scoring.

We analyzed this package, and provided a score, details, and suggestions below.

  • completed on Feb 3, 2018
  • Dart: 2.0.0-dev.20.0
  • pana: 0.10.1


Describes how popular the package is relative to other packages. [more]
57 / 100
Code health derived from static analysis. [more]
98 / 100
Reflects how tidy and up-to-date the package is. [more]
80 / 100
Overall score:
Weighted score of the above. [more]


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:union_type/union_type.dart.


  • Maintain CHANGELOG.md.

    Changelog entries help clients to follow the progress in your code.

  • The description is too short.

    Add more detail about the package, what it does and what is its target use case. Try to write at least 60 characters.

  • Maintain an example.

    Create a short demo in the example/ directory to show how to use this package. Common file name patterns include: main.dart, example.dart or you could also use union_type.dart.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.19.0
matcher ^0.12.0+2 0.12.1+4
Transitive dependencies
path 1.5.1
stack_trace 1.9.1
Dev dependencies
test ^0.12.17+3