Flutter BLoCを軽く調べる
自分は最初からriverpodで始めたので必要ないかと思いましたが、たまに見かけるので。
BLoC
stands for Business Logic Components.
cubitが統合されてる
https://github.com/felangel/cubit
どんなコード?
https://bloclibrary.dev/#/coreconcepts?id=creating-a-cubit
class CounterCubit extends Cubit<int> { CounterCubit(int initialState) : super(initialState); }
StateNotifierに似てる
https://pub.dev/packages/state_notifier
stateを変更する場合はemitを使う
class CounterCubit extends Cubit<int> { CounterCubit() : super(0); void increment() => emit(state + 1); }
stateを変更するStateNotifierとはちょっと違う
Just like with Cubit, a Bloc is a special type of Stream,
https://bloclibrary.dev/#/coreconcepts?id=stream-usage-1
enum CounterEvent { increment, decrement }
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0);
@override
Stream<int> mapEventToState(CounterEvent event) async* {
switch (event) {
case CounterEvent.decrement:
yield state - 1;
break;
case CounterEvent.increment:
yield state + 1;
break;
}
}
}
caseでメソッドを判定したり、yieldを使ったり結構癖がある気がするが、 全てがstreamで表現されるのは統一性があるのかもしれない。
それと中でproviderを使ってる
https://github.com/felangel/bloc/blob/master/packages/flutter_bloc/pubspec.yaml#L16
providerがメンテされなくなったらどうなるのだろうか?