sealed_class_generator 3.0.0

Sealed Class Generator #

sealed_class: pub package
sealed_class_generator: pub package

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: 3.0.0

dev_dependencies:
  sealed_class_generator: 3.0.0
  build_runner: X.X.X

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{}

Your subclasses should implement your sealed class.

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

class Failure implements Result {
  final String errorMessage;

  Failure(this.errorMessage);
}

class 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 {}

class Failure implements Result {
  final String errorMessage;

  Failure(this.errorMessage);
}

class 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_generator: ^3.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support 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_generator/sealed_class_generator.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
22
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
70
Overall:
Weighted score of the above. [more]
54
Learn more about scoring.

We analyzed this package on Feb 13, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.5

Health issues and suggestions

Document public APIs. (-1 points)

2 out of 2 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.

Fix lib/src/visitor.dart. (-1 points)

Analysis of lib/src/visitor.dart reported 2 hints:

line 5 col 10: Don't explicitly initialize variables to null.

line 9 col 43: 'name' is deprecated and shouldn't be used. Check element, or use getDisplayString().

Fix lib/src/gen.dart. (-0.50 points)

Analysis of lib/src/gen.dart reported 1 hint:

line 24 col 29: 'name' is deprecated and shouldn't be used. Check element, or use getDisplayString().

Format lib/src/transformer.dart.

Run dartfmt to format lib/src/transformer.dart.

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.

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_generator.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.6.0 <3.0.0
analyzer ^0.39.1 0.39.4
build ^1.2.1 1.2.2
sealed_class ^3.0.0 3.0.0
source_gen ^0.9.4+6 0.9.4+7
Transitive dependencies
_fe_analyzer_shared 1.0.3
args 1.5.2
async 2.4.0
charcode 1.1.3
collection 1.14.12
convert 2.1.1
crypto 2.1.4
csslib 0.16.1
dart_style 1.3.3
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
logging 0.11.4
meta 1.1.8
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0 2.0.0
path 1.6.4
pedantic 1.9.0
pub_semver 1.4.3
source_span 1.6.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+13
yaml 2.2.0
Dev dependencies
build_runner ^1.7.1
build_test ^0.10.9+1
test ^1.9.3