simple_bloc 1.1.8

  • Readme
  • Changelog
  • Example
  • Installing
  • 66


A simple alternative to the BLoC implementation created in Dimension C-137

Version

Getting started #

You should ensure that you add the simple_bloc as a dependency in your flutter project.

read more at how to install

Usage #

Simple BLoC for counter app


class CounterBloc extends Bloc {
  //define the controller
  final _counterController = BlocController<int>(initalData: 0);

  //output
  Stream<int> get counterOut => _counterController.stream;

  //action
  Action get increment => _counterController.action((value) {
        //implement your business logic here
        return ++value;
      });

  @override
  void dispose() {
    //dispose your controller
    _counterController.dispose();
  }
}

Screen Widget

import 'package:flutter/material.dart';
import 'package:simple_bloc/simple_bloc.dart';
import 'counter_bloc.dart';

class CounterScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //get bloc form provider
    final bloc = BlocProvider.of<CounterBloc>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("Simple BLoC"),
      ),
      body: Center(
        //consume output
        child: StreamBuilder(
          stream: bloc.counterOut,
          builder: (_, snapshot) {
            return Text(snapshot.data.toString());
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        //call bloc action
        onPressed: bloc.increment,
      ),
    );
  }
}

checkout app source at github

[1.1.8] - Aug 30, 2019

  • add getter for isClosed and isPaused to controller
  • updated packages dependencies

[1.1.7] - Aug 25, 2019

  • add method to controller

[1.1.6] - Aug 08, 2019

  • instantiate the blocs on initState

[1.1.5] - Aug 08, 2019

  • onDispose listenable on Bloc
  • BlocController can listen Bloc dispose

[1.1.4] - Aug 07, 2019

  • Builder constructor for BlocProvider

[1.1.3] - Aug 05, 2019

  • refactor Action and Input typedefs
  • implmented hasValue and value props

[1.1.2] - Aug 01, 2019

  • initalize method on BLoC is optional
  • updated dependencies

[1.1.1] - May 29, 2019

  • Multiple Providers support

[1.1.0] - May 19, 2019

  • initialize method for BLoC

[1.0.2] - May 19, 2019

  • Getter for BuildContext inside BLoC instance

[1.0.1] - May 17, 2019

  • Documented example

[1.0.0] - May 16, 2019

  • Initial version

example/README.md

example #

A Counter app with simple_bloc

Getting Started #

example/counter_bloc.dart

import 'package:simple_bloc/simple_bloc.dart';

class CounterBloc extends Bloc {
  //define the controller
  final _counterController = BlocController<int>(initalData: 0);

  //output
  Stream<int> get counterOut => _counterController.stream;

  //action
  Action get increment => _counterController.action((value) {
        //implement your business logic here
        return ++value;
      });

  @override
  void dispose() {
    //dispose your controller
    _counterController.dispose();
  }
}

example/counter_screen.dart

import 'package:flutter/material.dart';
import 'package:simple_bloc/simple_bloc.dart';
import 'counter_bloc.dart';

class CounterScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //get bloc form provider
    final bloc = BlocProvider.of<CounterBloc>(context);
    return Scaffold(
      appBar: AppBar(
        title: Text("Simple BLoC"),
      ),
      body: Center(
        //consume output
        child: StreamBuilder(
          stream: bloc.counterOut,
          builder: (_, snapshot) {
            return Text(snapshot.data.toString());
          },
        ),
      ),
      floatingActionButton: FloatingActionButton(
        child: Icon(Icons.add),
        //call bloc action
        onPressed: bloc.increment,
      ),
    );
  }
}

example/main.dart


import 'package:flutter/material.dart';
import 'package:simple_bloc/simple_bloc.dart';
import 'counter_bloc.dart';
import 'counter_screen.dart';

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    //wrap with the BlocProvider
    return BlocProvider(
      //add your blocs
      blocs: [BlocBuilder(() => CounterBloc())],
      child: MaterialApp(
        title: 'Simple BLoC Example',
        home: CounterScreen()
      ),
    );
  }
}


Use this package as a library

1. Depend on it

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


dependencies:
  simple_bloc: ^1.1.8

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

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

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

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Health issues and suggestions

Document public APIs. (-0.19 points)

35 out of 36 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 (rxdart).

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
provider ^3.1.0 3.1.0+1
rxdart ^0.22.2 0.22.6 0.23.0-dev.1
Transitive dependencies
collection 1.14.11 1.14.12
meta 1.1.7 1.1.8
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test