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

outdated

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

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 currentState, CounterEvent event) async* {
    if (event is Increment) {
      /// Simulating Network Latency
      await Future<void>.delayed(Duration(seconds: 1));
      yield currentState + 1;
    }
    if (event is Decrement) {
      /// Simulating Network Latency
      await Future<void>.delayed(Duration(milliseconds: 500));
      yield currentState - 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());
}
2711
likes
0
pub points
99%
popularity

Publisher

verified publisherbloclibrary.dev

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

Homepage

License

unknown (LICENSE)

Dependencies

meta, rxdart

More

Packages that depend on bloc