Soms
Soms

Reputation: 53

Listen to Events instead of States with Flutter BLOC

I am using the Flutter BLOC library (https://pub.dev/packages/bloc) I know there is a way to "listen to" BLOC states changes (with the listen() function)

chatBloc.listen((chatState) async {
      if (chatState is ChatStateInitialized) {
        // do something
      }
    });

But is there a way to listen to BLOC events instead ? Just like I would do with a classical StreamController ? Thanks to all who will be willing to help :-)

Julien

Upvotes: 5

Views: 4557

Answers (2)

AlejandroQS
AlejandroQS

Reputation: 448

Updated for version 7/8

Listening to changes in a bloc

If you need to listen only for one Bloc changes, you can override void onChange(Change<State> change) inside the desired Bloc class.

enum CounterEvent { increment }

class CounterBloc extends Bloc<CounterEvent, int> {
  CounterBloc() : super(0);

  @override
  Stream<int> mapEventToState(CounterEvent event) async* {
    switch (event) {
      case CounterEvent.increment:
        yield state + 1;
        break;
    }
  }

  @override
  void onChange(Change<int> change) {
    print(change);
    super.onChange(change);
  }
}

Listening to multiple bloc changes

If you need to listen changes globally on multiple Blocs, you can use the BlocObserver class.

class SimpleBlocObserver extends BlocObserver {
  @override
  void onChange(BlocBase bloc, Change change) {
    super.onChange(bloc, change);
    print('${bloc.runtimeType} $change');
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    super.onTransition(bloc, transition);
    print('${bloc.runtimeType} $transition');
  }

  @override
  void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
    print('${bloc.runtimeType} $error $stackTrace');
    super.onError(bloc, error, stackTrace);
  }
}

Then, you need to initialize it.

void main() {
  Bloc.observer = SimpleBlocObserver();
  CounterBloc()
    ..add(CounterEvent.increment)
    ..close();
}

More info on the Flutter Bloc library documentation.

Upvotes: 2

Taleb
Taleb

Reputation: 2259

Yes, you can listen to BLoC events by below code:

BlocSupervisor.delegate = MyBlocDelegate();

And your main.dart similar to below code:

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  BlocSupervisor.delegate = MyBlocDelegate();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: BlocProvider<CounterBLoC>(
        create: (ctx) => CounterBLoC(),
        child: TestBlocWidget(),
      ),
    );
  }
}

Here is your bloc_delegate.dart for listen to events of BLoC:

import 'package:bloc/bloc.dart';

class MyBlocDelegate extends BlocDelegate {
  @override
  void onEvent(Bloc bloc, Object event) {
    print(event);
    super.onEvent(bloc, event);
  }

  @override
  void onError(Bloc bloc, Object error, StackTrace stackTrace) {
    print(error);
    super.onError(bloc, error, stackTrace);
  }

  @override
  void onTransition(Bloc bloc, Transition transition) {
    print(transition);
    super.onTransition(bloc, transition);
  }
}

Upvotes: 3

Related Questions