Giter VIP home page Giter VIP logo

dart_sealed_unions's Introduction

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

dart_sealed_unions's People

Contributors

brianegan avatar jankn avatar nodinosaur avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

dart_sealed_unions's Issues

Generator

Hey, how about to add a code generator to avoid the huge amount of boiler plate ?

Default toString

Hi! I'm using this package and when writing tests, I often get failed messages like:

<instance of MySealedUnion> does not match <instance of MySealedUnion>

which makes it a little difficult to debug. My proposed solution is an override of the toString method for every union that looks like the following:

class Union4Impl<A, B, C, D> {
  @override
  String toString() => "$union4Impl:${join((a) => a.toString(), (b) => b.toString(), (c) => c.toString(), (d) => a.toString())}";

all the information still stays: this is a union 4 impl, but we pass down the toString to the actual class that is there.

let me know if we're open to a PR :)

Allow to filter which sealed unions you are listening

I know this is particular to flutter, but maybe it makes sense to you too.
Currently, we are able to call .join to listen all states, but sometimes we need a special handling for just one or two states.
For instance the code below. I had to use multiple empty closures to listen to the error state and be able to show a SnackBar. Sadly, in flutter, we can't just return a SnackBar to the view tree. The same for Alerts and to navigation.

I see two options here. to create siblings methods to .join to allow filtering of the current state or to expose the current state type being emitted. I know this library is meant to allow a centralized state management, but it looks like that it is not always possible in Flutter.

body: BlocListener<LoginBloc, LoginState>(
            listener: (context, state) {
              state.join((_) {}, (_) {}, (_) {}, (error) {
                _scaffoldKey.currentState.showSnackBar(
                  SnackBar(
                    content: Text('Fuuuuuuuuuuuu $error'),
                  ),
                );
              });
            },
            child: BlocBuilder<LoginBloc, LoginState>(
              builder: (context, state) {
                return Stack(
                  children: <Widget>[
                    _buildLoginForm(),
                    state.join(
                        (initial) => Container(),
                        (loading) => Positioned.fill(
                            child: Center(child: CircularProgressIndicator())),
                        (logged) => Container(),
                        (error) => Container()),
                  ],
                );
              },
            ),
          )

Am I missing something? Is there any other way to handle this?
Thanks.

cc: @felangel , maybe you have some input to this.

Edit: Simplified code.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.