相关疑难解决方法(0)

在 Flutter 中请求位置时,BLoC 不会产生状态

我使用三个 Flutter 包来实现一个功能,用户可以通过拉动刷新,使用 BLoC 逻辑检索地理坐标并将其传递回 Flutter。

  1. 拉动刷新
  2. 集团
  3. 地理定位器

问题是,当我在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_pa​​ge.dart …

dart flutter

4
推荐指数
1
解决办法
4946
查看次数

标签 统计

dart ×1

flutter ×1