ResoCoder / flutter-ddd-firebase-course

https://resocoder.com/flutter-firebase-ddd-course
GNU General Public License v3.0
470 stars 161 forks source link

Bloc To Bloc Communication #14

Closed mwenechac closed 4 years ago

mwenechac commented 4 years ago

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(); } }