sealed_class 1.0.0

Sealed Class Generator #

Fix Dart's lack of a sealed class.
Inspired by Kotlin's implementation and by Sealed Unions.
With this lib you can generate an unlimited amount of sealed subclasses.

Installation #

dependencies:
  sealed_class: 1.0.0

dev_dependencies:
  sealed_class_generator: 1.0.0
  build_runner: 1.0.0

How to use: #

A complete example is found in the example.

Define your Sealed class by annotating it with @Sealed()

part 'yourfilename.g.dart';

@Sealed()
abstract class Result{}

Add the subclasses to the annotation.

part 'yourfilename.g.dart';

@Sealed([Failure, Success])
abstract class Result{}

Let your subclasses mixin the generated file.
Let your sealed class implement the generated mixin.

@Sealed([Failure, Success])
abstract class Result implements $Result {}

class Failure with $Failure implements Result {
  final String errorMessage;

  Failure(this.errorMessage);
}

class Success with $Success implements Result {
  final String data;

  Success(this.data);
}

The complete file should now look like this:

import 'package:sealed_class/sealed_class.dart';

part 'yourfilename.g.dart';

@Sealed([Failure, Success])
abstract class Result implements $Result {}

class Failure with $Failure implements Result {
  final String errorMessage;

  Failure(this.errorMessage);
}

class Success with $Success implements Result {
  final String data;

  Success(this.data);
}

To make it compile run the following command in the terminal:

flutter packages pub run build_runner build --delete-conflicting-outputs

You can do:

void main() {
  final someResult = Success("Woohoo Sealed classes in Dart");
  someResult.continued(
    (failure) {
      print("Oh no it failed with errorMessage: ${failure.errorMessage}");
    },
    (success) {
      print(success.data);
    },
  );

  //Or if you want to reduce it to another value use `join`
  final someOtherResult = Failure("Some Error Message");
  final mapResult = someOtherResult.join(
    (failure) => failure.errorMessage,
    (success) => success.data,
  );
  print(mapResult);
}

Use this package as a library

1. Depend on it

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


dependencies:
  sealed_class: ^1.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

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

3. Import it

Now in your Dart code, you can use:


import 'package:sealed_class/sealed_class.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
0
Health:
Code health derived from static analysis. [more]
99
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
50
Overall:
Weighted score of the above. [more]
40
Learn more about scoring.

We analyzed this package on Nov 28, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.1
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

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

Health issues and suggestions

Document public APIs. (-1 points)

4 out of 4 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Maintenance issues and suggestions

Provide a file named CHANGELOG.md. (-20 points)

Changelog entries help developers follow the progress of your package. See the example generated by stagehand.

The package description is too short. (-20 points)

Add more detail to the description field of pubspec.yaml. Use 60 to 180 characters to describe the package, what it does, and its target use case.

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and sealed_class.dart. Packages with multiple examples should provide example/README.md.

For more information see the pub package layout conventions.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.5.0 <3.0.0
Dev dependencies
test ^1.9.3