Slide 1

Slide 1 text

BLoC A predictable state management library

Slide 2

Slide 2 text

BLoC A predictable state management library Android Dev Transit App Samuel Dionne

Slide 3

Slide 3 text

Agenda Why BLoC? Core Concepts Cubit VS Bloc Todo App demo 1 2 3 4

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

Streams Core Concepts

Slide 7

Slide 7 text

What is a Stream

Slide 8

Slide 8 text

What is a Stream Imagine a river

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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.

Slide 14

Slide 14 text

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.

Slide 15

Slide 15 text

Cubit Core Concepts

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

class CounterCubit extends Cubit { CounterCubit() : super(0); void increment() => emit(state + 1); }


Slide 18

Slide 18 text

class CounterCubit extends Cubit { CounterCubit() : super(0); void increment() => emit(state + 1); }


Slide 19

Slide 19 text

class CounterCubit extends Cubit { CounterCubit() : super(0); void increment() => emit(state + 1); }


Slide 20

Slide 20 text

class CounterCubit extends Cubit { CounterCubit() : super(0); void increment() => emit(state + 1); }


Slide 21

Slide 21 text

void main() { final cubit = CounterCubit(); print(cubit.state); // 0 cubit.increment(); print(cubit.state); // 1 cubit.close(); }


Slide 22

Slide 22 text

@override Widget build(BuildContext context) { return BlocProvider( create: (_) => CounterCubit(), child: const CounterView(), ); } BlocBuilder( builder: (context, state) { return Text('$state'); },

Slide 23

Slide 23 text

create: (_) => CounterCubit(), child: const CounterView(), ); } BlocBuilder( builder: (context, state) { return Text('$state'); }, ) FloatingActionButton( child: const Icon(Icons.add),

Slide 24

Slide 24 text

builder: (context, state) { return Text('$state'); }, ) FloatingActionButton( child: const Icon(Icons.add), onPressed: () { context.read().increment(); }, )

Slide 25

Slide 25 text

Bloc Core Concepts

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

builder: (context, state) { return Text('$state'); }, ) FloatingActionButton( child: const Icon(Icons.add), onPressed: () { const event = CounterIncrementPressed(); context.read().add(event); }, )

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

Todo App demo The ages old sample app

Slide 34

Slide 34 text

Get started with BLoC and helpful packages Resources https://bloclibrary.dev/ https://pub.dev/packages/freezed https://pub.dev/packages/hydrated_bloc https://cli.vgv.dev/ + + + +

Slide 35

Slide 35 text

THANK YOU!