Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bloc To Bloc Communication #14

Closed
mwenechac opened this issue Oct 11, 2020 · 0 comments
Closed

Bloc To Bloc Communication #14

mwenechac opened this issue Oct 11, 2020 · 0 comments

Comments

@mwenechac
Copy link

mwenechac commented Oct 11, 2020

How can I achieve bloc to bloc communication between note watcher bloc and note form bloc if I want to get a form value from the form and use it in the repository to filter notes on firestore?

Here is my implementation:
Its the same as you have done it here, its just the names that are different. When I listen on the bloc, I am not getting anything, its not getting called. Any comment will be appreciated.

import 'dart:async';

import 'package:bloc/bloc.dart';
import 'package:dartz/dartz.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:injectable/injectable.dart';
import 'package:kt_dart/collection.dart';
import 'package:meta/meta.dart';
import 'package:tola/application/trips/booking_form/booking_form_bloc.dart';
import 'package:tola/domain/trips/i_trip_repository.dart';
import 'package:tola/domain/trips/trip.dart';
import 'package:tola/domain/trips/trip_failure.dart';

part 'trip_watcher_event.dart';

part 'trip_watcher_state.dart';

part 'trip_watcher_bloc.freezed.dart';

@Injectable
class TripWatcherBloc extends Bloc<TripWatcherEvent, TripWatcherState> {
TripWatcherBloc(this._tripRepository, this.bookingFormBloc)
: super(const TripWatcherState.initial()) {
bookingFormBlocBlocSubscription = bookingFormBloc.listen((state) {
print('booking form bloc here');
departureTown = state.destinationTownField.getOrCrash();
destinationTown = state.destinationTownField.getOrCrash();
passengerCount = state.passengersField.getOrCrash() as int;
travelDate = state.dateField.getOrCrash() as DateTime;
});
}

final ITripRepository _tripRepository;
final BookingFormBloc bookingFormBloc;

String departureTown;
String destinationTown;
int passengerCount;
DateTime travelDate;

StreamSubscription bookingFormBlocBlocSubscription;

@OverRide
Stream mapEventToState(
TripWatcherEvent event,
) async* {
yield* event.map(watchAllStarted: (e) async* {

  print(travelDate);

  _tripRepository
      .watchAll(
          destinationTown: destinationTown,
          departureTown: departureTown,
          passengerCount: passengerCount,
          travelDate: travelDate)
      .listen(
        (failureOrTrips) =>
            add(TripWatcherEvent.tripsReceived(failureOrTrips)),
      );
}, tripsReceived: (e) async* {
  yield e.failureOrTrips.fold((l) => TripWatcherState.loadFailure(l),
      (r) => TripWatcherState.loadSuccess(r));
});

}

@OverRide
Future close() {
bookingFormBlocBlocSubscription.cancel();
return super.close();
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant