bloc 0.7.1 copy "bloc: ^0.7.1" to clipboard
bloc: ^0.7.1 copied to clipboard

outdated

The goal of this package is to make it easy to implement the BLoC Design Pattern (Business Logic Component).

example/main.dart

import 'package:bloc/bloc.dart';

abstract class CounterEvent {}

class Increment extends CounterEvent {
  @override
  String toString() => 'Increment';
}

class Decrement extends CounterEvent {
  @override
  String toString() => 'Decrement';
}

class CounterBloc extends Bloc<CounterEvent, int> {
  @override
  int get initialState => 0;

  @override
  Stream<int> mapEventToState(int state, CounterEvent event) async* {
    if (event is Increment) {
      /// Simulating Network Latency
      await Future<void>.delayed(Duration(seconds: 1));
      yield state + 1;
    }
    if (event is Decrement) {
      /// Simulating Network Latency
      await Future<void>.delayed(Duration(milliseconds: 500));
      yield state - 1;
    }
  }
}

class SimpleBlocDelegate implements BlocDelegate {
  @override
  void onTransition(Transition transition) {
    print(transition.toString());
  }
}

void main() {
  BlocSupervisor().delegate = SimpleBlocDelegate();

  final counterBloc = CounterBloc();

  counterBloc.dispatch(Increment());
  counterBloc.dispatch(Increment());
  counterBloc.dispatch(Increment());

  counterBloc.dispatch(Decrement());
  counterBloc.dispatch(Decrement());
  counterBloc.dispatch(Decrement());
}
3.09k
likes
0
points
2.51M
downloads

Publisher

verified publisherbloclibrary.dev

Weekly Downloads

The goal of this package is to make it easy to implement the BLoC Design Pattern (Business Logic Component).

Homepage
Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

meta, rxdart

More

Packages that depend on bloc