Upgrade to Pro — share decks privately, control downloads, hide ads and more …

April Flutter Meetup - BLoC State Management

GDG Montreal
April 18, 2023
22

April Flutter Meetup - BLoC State Management

GDG Montreal

April 18, 2023
Tweet

Transcript

  1. know what state our application is in at any point

    in time easily test every case to make sure our app is responding appropriately record every user interaction so that we can make data-driven decisions work as efficiently as possible and reuse components develop fast and reactive apps + + + + + Why BLoC As developers we want to
  2. Simple Easy to understand & can be used by developers

    with varying skill levels. Powerful Help make amazing, complex applications by composing them of smaller components. Testable Easily test every aspect of an application so that we can iterate with confidence. Values
  3. Every now and then, a sheet of paper floats down

    to you. What is a Stream Imagine a river
  4. Every now and then, a sheet of paper floats down

    to you. What is a Stream Imagine a river
  5. Every now and then, a sheet of paper floats down

    to you. What is a Stream Imagine a river
  6. Every now and then, a sheet of paper floats down

    to you. What is a Stream Imagine a river
  7. What is a Stream Imagine a river Every now and

    then, a sheet of paper floats down to you. All those sheets of paper are async data coming down the stream.
  8. State State State State What is a Stream Imagine a

    river Every now and then, a sheet of paper floats down to you. All those sheets of paper are async data coming down the stream. In the world of BLoC, those sheets of papers represent the State of your application.
  9. States Outputted in a stream. UI components can be notified

    of states changes and redraw themselves. Functions Invoked to trigger state changes. Cubit states functions
  10. void main() { final cubit = CounterCubit(); print(cubit.state); // 0

    cubit.increment(); print(cubit.state); // 1 cubit.close(); }

  11. @override Widget build(BuildContext context) { return BlocProvider( create: (_) =>

    CounterCubit(), child: const CounterView(), ); } BlocBuilder<CounterCubit, int>( builder: (context, state) { return Text('$state'); },
  12. create: (_) => CounterCubit(), child: const CounterView(), ); } BlocBuilder<CounterCubit,

    int>( builder: (context, state) { return Text('$state'); }, ) FloatingActionButton( child: const Icon(Icons.add),
  13. builder: (context, state) { return Text('$state'); }, ) FloatingActionButton( child:

    const Icon(Icons.add), onPressed: () { context.read<CounterCubit>().increment(); }, )
  14. States Outputted in a stream. UI components can be notified

    of states changes and redraw themselves. Events Received as stream, they get converted into state changes. Bloc states events
  15. abstract class CounterEvent {} class CounterIncrementPressed extends CounterEvent {} class

    CounterBloc extends Bloc<CounterEvent, int> { CounterBloc() : super(0){ on<CounterIncrementPressed>((event, emit) { emit(state + 1); }); } }
  16. abstract class CounterEvent {} class CounterIncrementPressed extends CounterEvent {} class

    CounterBloc extends Bloc<CounterEvent, int> { CounterBloc() : super(0){ on<CounterIncrementPressed>((event, emit) { emit(state + 1); }); } }
  17. abstract class CounterEvent {} class CounterIncrementPressed extends CounterEvent {} class

    CounterBloc extends Bloc<CounterEvent, int> { CounterBloc() : super(0){ on<CounterIncrementPressed>((event, emit) { emit(state + 1); }); } }
  18. abstract class CounterEvent {} class CounterIncrementPressed extends CounterEvent {} class

    CounterBloc extends Bloc<CounterEvent, int> { CounterBloc() : super(0){ on<CounterIncrementPressed>((event, emit) { emit(state + 1); }); } }
  19. builder: (context, state) { return Text('$state'); }, ) FloatingActionButton( child:

    const Icon(Icons.add), onPressed: () { const event = CounterIncrementPressed(); context.read<CounterBloc>().add(event); }, )
  20. Cubits are simple and quick to start with. Blocs are

    more complexe, but come with the added benefit of Traceability. Meaning you know the sequence of state changes as well as exactly what triggered those changes. Using stream for events allow us to take advantage of stream operators such as buffer, debounceTime, throttle, etc. Worry not, because it’s easy to refactor and scale-up from a Cubit to a Bloc. Cubit VS Bloc When would we use one over the other