Created
July 29, 2020 08:38
-
-
Save magicleon94/42837a1dda02111bb0abe0b9bf1cac58 to your computer and use it in GitHub Desktop.
BLoC to BLoC communication example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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