sealed_generator 1.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 59

ᶘ ᵒᴥᵒᶅ

SealedCodegen - 'when' operator generator for @sealed classes #

Getting started #

  1. Add these dependencies
dev_dependencies:
  build_runner: ^1.0.0
  sealed_generator: 1.0.1
  1. Mark any class you want with @sealed annotation (meta: ^1.1.7) and add part '
import 'package:meta/meta.dart';

part 'result.g.dart';

@sealed
class Result<T> with SealedResult<T> {}

class Success<T> extends Result<T> {
  T value;

  Success(this.value);
}
  1. run:
flutter packages pub run build_runner build

Generator will create a class OriginalClassNameSealed for you to use

class SealedResult<T> {
  R when<R>({
      @required R Function(Success<T>) success,
      @required R Function(Failure<T>) failure,
    }) {
    if (this is Success<T>) {
      return success(this as Success<T>);
    }
    if (this is Failure<T>) {
      return failure(this as Failure<T>);
    }
    throw new Exception(
        'If you got here, probably you forgot to regenerate the classes? Try running flutter packages pub run build_runner build');
  }
}
  1. Add with(or extends) to your sealed class, for e.g. class Result extends(with) ResultSealed

Using #

Just create an instance of you sealed class and call when on it, for example:

    var resultWidget = result.when(
        success: (event) => Text(event.value),
        failure: (event) => Text("Failure"),
        idle: (event) => Text("idle"),
      );

And that's it, you are ready to use sealed classses with some sort of when

It is a very early version of the library, mostly a proof of concept, so contributions are highly welcomed

1.0.1 #

  • Fix version resolving

1.0.0 #

  • Initial release

example/lib/main.dart

import 'dart:math';

import 'package:flutter/material.dart';
import 'package:sealed_demo/result.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Result<String> result = Idle();

  void _changeState() {
    setState(() {
      var value = Random().nextInt(3);
      if (value == 0) {
        result = Failure();
      } else if (value == 1) {
        result = Success<String>("Value from success");
      } else if (value == 2) {
        result = Idle();
      }
    });
  }

  @override
  Widget build(BuildContext context) {
    var resultWidget = result.when(
      success: (event) => Text(event.value),
      failure: (event) => Text("Failure"),
      idle: (event) => Text("idle"),
    );

    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            resultWidget,
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _changeState,
        tooltip: 'Increment',
        child: Icon(Icons.add),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  sealed_generator: ^1.0.1

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

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

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

  • Dart: 2.5.1
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.4

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

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.

Maintenance issues and suggestions

Support latest dependencies. (-10 points)

The version constraint in pubspec.yaml does not support the latest published versions for 1 dependency (analyzer).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
analyzer >=0.34.0 <=0.38.3 0.38.3 0.39.0
build ^1.1.5 1.2.0
flutter 0.0.0
meta ^1.1.7 1.1.7
recase ^2.0.1 2.0.1
source_gen ^0.9.4+3 0.9.4+5
Transitive dependencies
args 1.5.2
async 2.4.0
charcode 1.1.2
collection 1.14.11 1.14.12
convert 2.1.1
crypto 2.1.3
csslib 0.16.1
dart_style 1.3.2
front_end 0.1.25 0.1.28
glob 1.2.0
html 0.14.0+3
js 0.6.1+1
kernel 0.3.25 0.3.28
logging 0.11.3+2
node_interop 1.0.3
node_io 1.0.1+2
package_config 1.1.0
path 1.6.4
pedantic 1.8.0+1
pub_semver 1.4.2
sky_engine 0.0.99
source_span 1.5.5
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
vector_math 2.0.8
watcher 0.9.7+12
yaml 2.2.0
Dev dependencies
build_runner ^1.0.0
build_test ^0.10.0
flutter_test
test ^1.0.0