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

outdated

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

example/lib/main.dart

import 'dart:async';

import 'package:bloc/bloc.dart';

abstract class CounterEvent {}

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

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

class CounterBloc extends Bloc<CounterEvent, int> {
  void increment() {
    dispatch(IncrementCounter());
  }

  void decrement() {
    dispatch(DecrementCounter());
  }

  @override
  int get initialState => 0;

  @override
  void onTransition(Transition<CounterEvent, int> transition) {
    print(transition.toString());
  }

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

void main() async {
  final counterBloc = CounterBloc();

  // Increment Phase
  counterBloc.dispatch(IncrementCounter());
  counterBloc.dispatch(IncrementCounter());
  counterBloc.dispatch(IncrementCounter());

  // Decrement Phase
  counterBloc.dispatch(DecrementCounter());
  counterBloc.dispatch(DecrementCounter());
  counterBloc.dispatch(DecrementCounter());
}
2898
likes
0
pub points
99%
popularity

Publisher

verified publisherbloclibrary.dev

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