Pjoter
Pjoter

Reputation: 13

Transform Freezed Events in BLoC

I'd like to know if there's a way to properly set BLoC Events using Freezed library. Problem appears when you want to add transformer into selected events.

Let's consider this scenario:

class CustomState {}

@freezed
class CustomEvent with _$CustomEvent {
  const factory CustomEvent.regularEvent() = _RegularEvent;
  const factory CustomEvent.eventWithTransformer() = _EventWithTransformer;
}

class CustomBloc extends Bloc<CustomEvent, CustomState> {
  CustomBloc(CustomState initialState) : super(initialState) {
    on<CustomEvent>((event, emitter) {
      //handle Events
    },
        transformer: transformOnlyEventWithTransformer());
  }
}

How can I add transform just for eventWithTransformer ?

on<CustomEvent.regularEvent>((event, emitter) {
   //this won't work as ```CustomEvent.regularEvent``` is not a type
}

Also, you cannot have two on<> with same event in one bloc so below won't work either

on<CustomEvent>((event, emitter) {
   event.whenOrNull(
     regularEvent: () => // handle event
  )
}

on<CustomEvent>((event, emitter) {
   event.whenOrNull(
     eventWithTransformer: () => // handle event
  )
},         transformer: transformOnlyEventWithTransformer());

Let me know if there's any solution to this issue. If not, it'd nice to add this a feature request.

Upvotes: 1

Views: 1574

Answers (1)

Jacob
Jacob

Reputation: 36

Had the similar issue problem, I was originally doing

on<CustomEvent.RegularEvent>((event, emitter) {})

I fixed it by changing it to

on<RegularEvent>((event, emitter) {})

You'll have to make your events public for it to work, I would like it work with it but it's fine for my case.

Upvotes: 2

Related Questions