我使用三个 Flutter 包来实现一个功能,用户可以通过拉动刷新,使用 BLoC 逻辑检索地理坐标并将其传递回 Flutter。
问题是,当我在pull-to-refresh 中发送调用时,我无法让 BLoC 产生返回结果。
geolocation_bloc.dart
class GeolocationBloc extends Bloc<GeolocationEvent, GeolocationState> {
@override
GeolocationState get initialState => GeolocationUninitialized();
@override
Stream<GeolocationState> mapEventToState(GeolocationEvent event) async* {
if (event is RequestLocation) {
yield* _mapGeolocationRequestLocation();
}
}
Stream<GeolocationState> _mapGeolocationRequestLocation() async* {
Position position;
position = await Geolocator().getCurrentPosition();
print("RETRIEVED LOCATION"); // I CAN REACH HERE EVERYTIME.
if (position == null) {
yield LocationLoaded(0, 0);
} else {
yield LocationLoaded(position.latitude, position.longitude);
}
}
Run Code Online (Sandbox Code Playgroud)
检索地理坐标。如果传感器关闭/损坏,则返回 (0,0)。
feed_page.dart …