Skip to content

Instantly share code, notes, and snippets.

@magicleon94
Created July 29, 2020 08:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magicleon94/42837a1dda02111bb0abe0b9bf1cac58 to your computer and use it in GitHub Desktop.
Save magicleon94/42837a1dda02111bb0abe0b9bf1cac58 to your computer and use it in GitHub Desktop.
BLoC to BLoC communication example
class AutocompleteQuery {
final String query;
final LatLng location;
final double radius;
AutocompleteQuery(this.query, {this.location, this.radius = 3000});
}
class AutocompleteBloc extends BlocBase {
final _autocompletePlacesInputSubject = PublishSubject<AutocompleteQuery>();
Sink<AutocompleteQuery> get autocompletePlacesSink =>
_autocompletePlacesInputSubject.sink;
AutocompleteBloc() {
_autocompletePlacesInputSubject
.debounce((event) => TimerStream(true, Duration(milliseconds: 500)))
.asyncMap(
(event) => GeolocationManager.instance
.getSuggestedPlacesByQuery(
event.query,
location: event.location,
radius: event.radius,
)
.catchError((e) => throw (e)),
)
.listen(_autocompletePlacesOutputSubject.sink.add, onError: (e) {
print(e);
_autocompletePlacesOutputSubject.sink.addError(e);
});
}
final _autocompletePlacesOutputSubject = BehaviorSubject<List<Place>>();
Stream<List<Place>> get autocompletePlacesStream =>
_autocompletePlacesOutputSubject.stream;
@override
void disposeInstance() {
_autocompletePlacesInputSubject?.close();
}
}
class PlacesBloc extends BlocBase implements AutocompleteBloc {
final PlacesService placesService;
final AutocompleteBloc _autocompleteBloc;
PlacesBloc(this.placesService, AutocompleteBloc autocompleteBloc)
: _autocompleteBloc = autocompleteBloc;
@override
Sink<AutocompleteQuery> get autocompletePlacesSink =>
_autocompleteBloc.autocompletePlacesSink;
@override
Stream<List<Place>> get autocompletePlacesStream =>
_autocompleteBloc.autocompletePlacesStream;
@override
void disposeInstance() {
_autocompleteBloc?.dispose();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment