Search code examples
flutterdartbloc

Migrate to BLoC 7.2- Nested Streams - yield* inside other stream


I'm migrating a project from Bloc 7.0 to 7.2

I have an issue trying handle the migration of this following Stream since it is calling another Stream within it self :

 Stream<CustomerState> _mapUpdateNewsletter({...}) async* {
    try {
      [...]
      
      yield* _mapGetCustomer(); // Calling another Stream here

      Toast.showSuccess(message: successMessage);
    } ...
  }

Here is what the called Stream used to look like

 Stream<CustomerState> _mapGetCustomer() async* {
    try {
      final customer = await _customerRepository.getCustomer();
      yield state.getCustomerSuccess(customer);
    } catch (error, stackTrace) {
      ApiError.handleApiError(error, stackTrace);
    }
  }

Here is what I migrated it to :

  Future<void> _onGetCustomer(
      GetCustomer event, Emitter<CustomerState> emit) async {
    try {
      final customer = await _customerRepository.getCustomer();
      emit(state.getCustomerSuccess(customer));
    } catch (error, stackTrace) {
      ApiError.handleApiError(error, stackTrace);
    }
  }

How am I suppose to call it now in Bloc 7.2 ?

 Future<void> _onUpdateNewsletter(UpdateNewsletter event, Emitter<CustomerState> emit) async {
    try {
     ...
     yield* _onGetCustomer; // How do I call this async future here?
      Toast.showSuccess(message: event.successMessage);
    } ...
}

Solution

  • in the new version of the bloc, you don't have to write stream functions. you have a function called emit and calling this function and passing the new state is possible from every function in your bloc. so remove yield* and just call _onGetCustomer function and from there emit your new state.