Flutter Mobx-从另一个商店的商店服务中观察属性

时间:2020-06-10 13:25:07

标签: flutter dart service store mobx

我目前有2个商店控制器,第一个是位置服务商店,每次移动电话时都会更新“ currentLocation”属性,第二个是第二个服务商店,每次从第一个商店获取位置属性时,该服务商店都需要触发一些操作已更新。

是否可以从其他商店观察商店中的财产?

LocationServiceStore:

import 'package:approachapp/app/customer/shared/location/location_coordinates.dart';
import 'package:geolocator/geolocator.dart';
import 'package:mobx/mobx.dart';

part 'location_controller.g.dart';

class LocationController = _LocationControllerBase with _$LocationController;

abstract class _LocationControllerBase with Store {
  Geolocator _geolocator = Geolocator();
  GeolocationStatus _statusGranted;

  _LocationControllerBase() {
    _startLocationService();
  }

  @observable
  LocationCoordinates currentLocation;

  @action
  void updateCurrentLocation(Position tempPosition) {
    LocationCoordinates tempLocation = new LocationCoordinates(
        latitude: tempPosition.latitude, longitude: tempPosition.longitude);
    currentLocation = tempLocation;

    print('${currentLocation.latitude}, ${currentLocation.longitude}');
  }

  Future<void> _startLocationService() async {
    LocationOptions locationOptions =
        LocationOptions(accuracy: LocationAccuracy.best, distanceFilter: 5);
    print("Starting GPS Location Service");

    _geolocator..forceAndroidLocationManager = true;
    _geolocator.getPositionStream(locationOptions).listen((position) {
      if (position?.latitude != null && position?.longitude != null) {
        updateCurrentLocation(position);
      } else {
        print('Position Invalid');
      }
    });
  }
}

我基本上希望我的第二家商店X每次更新“ currentLocation”属性时都执行一个函数。

谢谢!

0 个答案:

没有答案