bsev 2.1.0+1 copy "bsev: ^2.1.0+1" to clipboard
bsev: ^2.1.0+1 copied to clipboard

discontinued

Useful to aid in the use of BloC pattern with dependency injection

pub package

[DISCONTINUED] #

Our efforts are directed to the new package Cubes.

BSEV (BloC,Streams,Events,View) #

Set of packages and utilitarian functions that help in the use of BloC pattern with dependency injection.

With bsev you will use the Bloc pattern in a simple, reactive and organized way. Communication between the business logic and the view occurs entirely through two-way streams.

Usage #

To use this plugin, add bsev as a dependency in your pubspec.yaml file.

We should initially create the class Communication and Events:

Streams

import 'package:bsev/bsev.dart';

class HomeCommunication extends Communication{

  var count = BehaviorSubjectCreate<Int>(initValue: 0);
  //var count = StreamCreate<Int>();
  //var count = PublishSubjectCreate<Int>();
  //var count = ReplaySubjectCreate<Int>();

  @override
  void dispose() {
    count.close();
    super.dispose();
  }

}

Events

import 'package:bsev/bsev.dart';

class IncrementEvent extends EventsBase{}

Now we can create our Bloc, class that will be centralized the business rule.

Bloc

import 'package:bsev/bsev.dart';

class HomeBloc extends Bloc<HomeCommunication>{

  // If you need to communicate with some instantiated BloC, regardless of whether part of your tree of widgets can use:
  // dispatchToBloc<OtherBloc>(MsgEvent());

  // If you need send event to all BloCs
  // dispatchToAllBlocs(MyEvent());
  
  // If you need to send an event to the view:
  // dispatchView(MyEvent());

  @override
  void init() {
  // execute something when view is ready.
  }
  
  @override
  void eventReceiver(EventsBase event) {
  
  // called when the Bloc receives an event
  
    if(event is IncrementEvent){
      communication.count.set(streams.count.value + 1)
    }
    
  }
}

In our bloc we have 2 mandatory methods: initState and eventReceiver:

initView: In the first buildView this method is invoked;

eventReceiver: Invoked whenever the pad receives an event;

View

import 'package:bsev/bsev.dart';

class HomeView extends StatelessWidget{
  
  @override
  Widget build(BuildContext context) {
    
    return BsevBuilder<HomeBloc,HomeCommunication>(
      dataToBloc: "any data", //optional initial data to bloc
      eventReceiver: (event, communication){ //optional
        // performs action received by the bloc
      },
      builder: (context,communication){
      
          return Scaffold(
            appBar: AppBar(),
            body: communication.count.builder<int>((value) {
                return Text(value.toString());
            }),
            floatingActionButton: FloatingActionButton(
                onPressed: (){
                  communication.dispatcher(IncrementEvent());
                }
            ),
          );
      
      }
    );
    
  }
  
}

As our Bloc and our StreamsBase will be injected automatically, we should configure it in the Injector in the main of our application:

  MyApp(){

    registerBloc<HomeBloc, HomeCommunication>((i) => HomeBloc(i.get()), () => HomeCommunication());

    //if you want singleton
    //registerSingletonBloc<HomeBloc, HomeCommunication>((i) => HomeBloc(i.get()), () => HomeCommunication());

    //Example of the register any things.
    //registerDependency((i) => CryptoRepository(i.get()));

    //Example of the register any things Singleton.
    //registerSingletonDependency((i) => CryptoRepository(i.get()));

    //Example get dependency anywhere
    //var dependency = getDependency<CryptoRepository>();
    
  }

Questions about how to use the get_it consult documentation.

More complex example is found here

Testing HomeBloc #

import 'package:flutter_test/flutter_test.dart';

void main() {
  HomeCommunication _homeCommunication;

  setUp(() {
    _homeCommunication = HomeCommunication();
    _homeCommunication.setBloc(HomeBloc());
  });

  tearDown(() {
    _homeCommunication?.dispose();
  });
  
  test('initial streams', () {
    expect(_homeCommunication.count.value, 0);
  });
  
  test('increment value', () {
    _homeCommunication.dispatcher(IncrementEvent());
    expect(_homeCommunication.count.value, 1);
  });

  test('increment value 3 times', () {
    _homeCommunication.dispatcher(IncrementEvent());
    _homeCommunication.dispatcher(IncrementEvent());
    _homeCommunication.dispatcher(IncrementEvent());
    expect(_homeCommunication.count.value, 3);
  });
  
}

Test example with asynchronous call: here

Used packages #

Packages pub
rxdart Pub
get_it Pub

User cases #

Apps
DartLangBr
Boleiro
0
likes
35
pub points
0%
popularity

Publisher

unverified uploader

Useful to aid in the use of BloC pattern with dependency injection

Repository (GitHub)
View/report issues

License

MIT (LICENSE)

Dependencies

flutter, get_it, rxdart

More

Packages that depend on bsev