Bloc은 조금 더 나은 클래스로 events에 의존해서 state변경을 만듭니다.Bloc 은 또한 BlocBase를 extends하여 Cubit과 유사한 공통 Api가 있습니다.그러나 bloc은 새로운 state를 직접 emit하는 대신 event 를 수신하고 수신된 event를 나가는 state로 변환합니다.
Bloc 만들기
Bloc을 생성하는 것은 cubit을 생성하는 것과 비슷하지만, 관리할 state를 정의하는것 외에 bloc이 처리할 event도 정의해야 한다는 점이 다르다. event는 bloc에 대한 입력을 받는 것이다.
sealed class CounterEvent{}
final class CounterIncrementPressed extends CounterEvent{}
class CounterBloc extencds Bloc<CounterEvent, int> {
CounterBloc() : super(0);
}
Bloc의 state변화
Bloc 은 Cubit의 함수가 아닌 on<Event> Api를 통해 이벤트 핸들러를 등록해야 합니다. 이벤트 핸들러는 들어오는 모든 event를 0개 이상의 나가는 state로 변환하는 역활을 수행합니다.
sealed class CounterEvent {}
final class CounterIncrementPressed extends CounterEvent{}
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<CounterIncrementPressed>((event, emit){
// 여기서 CounterIncrementPressed 를 구현한다.
});
}
그런 다음 EventHandler를 업데이트 하여 CounterIncrementPressed 이벤트를 처리할 수 있다.
sealed class CounterEvent {}
final class CounterIncrementPressed extends CounterEvent{}
class CounterBloc extends Bloc<CounterEvent, int> {
CounterBloc() : super(0) {
on<CounterIncrementPressed>((event, emit){
// 여기서 CounterIncrementPressed 를 구현한다.
emit(state + 1);
});
}
cubit과 마찬가지로 각 Bloc에는 addError와 onError메서드가 있습니다. Bloc 내부 어디에서나 addError를 호출하여 에러가 발생했음을 알릴 수 있습니다. 그런 다음 Cubit과 마찬가지로 onError를 override하여 모든 에러에 대응할 수 있습니다.
* What went wrong: The Android Gradle plugin supports only Kotlin Gradle plugin version 1.5.20 and higher. The following dependencies do not satisfy the required version: project ':kakao_login' -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.4.32
위와 같은 오류가 떴다.
그냥 해석 하자면 내 프로젝트가 현재 코틀린 그레이들 플러그인 버전 1.5.20 보다 위를 지원하는데
kakao_login이 1.4.32를 써야 한다는 뜻!
android/build.gradle에 가서
'com.android.tools.build:gradle:7.3.0' 으로 되어 있던걸 com.android.tools.build:gradle:7.1.2 로 수정하면 된다!