sealed_flutter_bloc 2.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 77

logo
Pub Package Build Status Code Coverage Effective Dart MIT License

flutter_bloc meets sealed_unions


Quick Start #

Extend UnionNImpl

class MyState extends Union4Impl<Initial, Loading, Success, Failure> {
  static final unions = const Quartet<Initial, Loading, Success, Failure>();

  MyState._(Union4<Initial, Loading, Success, Failure> union) : super(union);

  factory MyState.initial() => MyState._(unions.first(Initial()));

  factory MyState.loading() => MyState._(unions.second(Loading()));

  factory MyState.success({String data}) =>
      MyState._(unions.third(Success(data: data)));

  factory MyState.failure({String error}) =>
      MyState._(unions.fourth(Failure(error: error)));
}

class Initial {}

class Loading {}

class Success {
  final String data;

  Success({this.data});
}

class Failure {
  String error;

  Failure({this.error});
}

Use SealedBlocBuilder

class MyWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return SealedBlocBuilder4<MyBloc, MyState, Initial, Loading, Success,
        Failure>(
      builder: (context, states) => states(
        (initial) => Text('Initial'),
        (loading) => CircularProgressIndicator(),
        (success) => Text('Success: ${success.data}'),
        (failure) => Text('Failure: ${failure.error}'),
      ),
    );
  }
}

Additional Examples #

More examples can be found at sealed_flutter_bloc_samples.

2.0.0 #

1.0.0 #

Update to bloc ^1.0.0

0.2.0 #

Update to bloc ^0.16.0

0.1.1 #

Minor Documentation Updates.

0.1.0 #

Initial Version of the library.

Includes:

  • SealedBlocBuilder...SealedBlocBuilder9

example/lib/main.dart

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:bloc/bloc.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:sealed_flutter_bloc/sealed_flutter_bloc.dart';

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

class MyState extends Union4Impl<Initial, Loading, Success, Failure> {
  static final unions = const Quartet<Initial, Loading, Success, Failure>();

  MyState._(Union4<Initial, Loading, Success, Failure> union) : super(union);

  factory MyState.initial() => MyState._(unions.first(Initial()));

  factory MyState.loading() => MyState._(unions.second(Loading()));

  factory MyState.success({String data}) =>
      MyState._(unions.third(Success(data: data)));

  factory MyState.failure({String error}) =>
      MyState._(unions.fourth(Failure(error: error)));
}

class Initial {}

class Loading {}

class Success {
  final String data;

  Success({this.data});
}

class Failure {
  String error;

  Failure({this.error});
}

enum MyEvent { load, error }

class MyBloc extends Bloc<MyEvent, MyState> {
  @override
  MyState get initialState => MyState.initial();

  @override
  Stream<MyState> mapEventToState(MyEvent event) async* {
    switch (event) {
      case MyEvent.load:
        yield MyState.loading();
        await Future<void>.delayed(Duration(seconds: 3));
        yield MyState.success(data: 'Some Data');
        break;
      case MyEvent.error:
        yield MyState.failure(error: 'oops!');
        break;
    }
  }
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: BlocProvider<MyBloc>(
        builder: (context) => MyBloc(),
        child: MyHome(),
      ),
    );
  }
}

class MyHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Home')),
      body: Center(
        child: SealedBlocBuilder4<MyBloc, MyState, Initial, Loading, Success,
            Failure>(
          builder: (context, states) => states(
            (initial) => Text('Initial'),
            (loading) => CircularProgressIndicator(),
            (success) => Text('Success: ${success.data}'),
            (failure) => Text('Failure: ${failure.error}'),
          ),
        ),
      ),
      floatingActionButton: Column(
        mainAxisAlignment: MainAxisAlignment.end,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: <Widget>[
          Padding(
            padding: EdgeInsets.all(8.0),
            child: FloatingActionButton(
              child: Icon(Icons.check),
              onPressed: () {
                BlocProvider.of<MyBloc>(context).add(MyEvent.load);
              },
            ),
          ),
          Padding(
            padding: EdgeInsets.all(8.0),
            child: FloatingActionButton(
              child: Icon(Icons.error),
              onPressed: () {
                BlocProvider.of<MyBloc>(context).add(MyEvent.error);
              },
            ),
          ),
        ],
      ),
    );
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  sealed_flutter_bloc: ^2.0.0

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_flutter_bloc/sealed_flutter_bloc.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
53
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
77
Learn more about scoring.

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

  • Dart: 2.6.0
  • pana: 0.12.21
  • Flutter: 1.9.1+hotfix.6

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0 <3.0.0
bloc ^2.0.0 2.0.0
flutter 0.0.0
flutter_bloc ^2.0.0 2.0.0
sealed_unions ^3.0.2+2 3.0.2+2
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7 1.1.8
provider 3.1.0+1
rxdart 0.22.6
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
effective_dart ^1.1.1
flutter_test