flutter 我如何创建一个带有工厂方法的类并将其作为单例使用?

gcuhipw9  于 2023-01-31  发布在  Flutter
关注(0)|答案(2)|浏览(133)

我试着创建一个有工厂方法的类,并把它作为一个单例类来使用。然而,我的类需要一些构造函数,这样做我总是不能正确地编写它
有可能传递构造函数并使用工厂方法吗?我还没能这么做。这是类

class LocationService {
  BuildContext context;
  late Position position;
  GeoPoint? initPosition;
  bool loadGeoFire = false;
  Function update_drivers;
  bool nearbyAvailableDriverKeysLoaded;
  bool removeDriverIcon;
  String? removeDriverKey;

  LocationService({
    required this.context,
    required this.update_drivers,
    required this.nearbyAvailableDriverKeysLoaded,
    required this.removeDriverIcon,
    required this.removeDriverKey,
    required this.initPosition,
    required this.loadGeoFire,
    // required this.position,
  });

  Future<void> locatePosition() async {
    final appData = context.read<AppData>();
    position = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);
    print("This is your Position:: " + position.toString());

    GeoPoint initPos =
        GeoPoint(latitude: position.latitude, longitude: position.longitude);

    initPosition = initPos;

    appData.initPosition = initPos;
    String address =
        // ignore: unnecessary_cast
        await (AssistantMethods.searchCoordinateAddress(position, context)
            as FutureOr<String>);
    print("This is your Address:: " + address);
    if (!loadGeoFire) {
      initializeGeoFire(
        position: position,
        update_drivers: update_drivers,
        nearbyAvailableDriverKeysLoaded: nearbyAvailableDriverKeysLoaded,
        removeDriverIcon: removeDriverIcon,
        removeDriverKey: removeDriverKey,
      );
      loadGeoFire = true;
    }
    // uName = userCurrentInfo.name;
    Provider.of<AppData>(context, listen: false).closeSplashScreen(false, true);
    AssistantMethods.retrieveHistoryInfo(context);
  }
}

我试过做这样的事

class LocationService {
  BuildContext context;
  late Position position;
  GeoPoint? initPosition;
  bool loadGeoFire = false;
  Function update_drivers;
  bool nearbyAvailableDriverKeysLoaded;
  bool removeDriverIcon;
  String? removeDriverKey;

  static final LocationService _instance = LocationService._internal(
    context: context,
    update_drivers: update_drivers,
    nearbyAvailableDriverKeysLoaded: nearbyAvailableDriverKeysLoaded,
    removeDriverIcon: removeDriverIcon,
    removeDriverKey: removeDriverKey,
    initPosition: initPosition,
    loadGeoFire: loadGeoFire,
    position: position,
  );

  factory LocationService() {
    return _instance;
  }

  LocationService._internal({
    required this.context,
    required this.update_drivers,
    required this.nearbyAvailableDriverKeysLoaded,
    required this.removeDriverIcon,
    required this.removeDriverKey,
    required this.initPosition,
    required this.loadGeoFire,
    required this.position,
  });

我不会工作虽然。不断得到错误The instance member 'context' can't be accessed in an initializer. Try replacing the reference to the instance member ..等我真的需要创建一个单例类吗?我调用locatePosition方法从不同的地方,读到这是一个很好的做法,并希望这样做。
这是我在另一个类中调用它的方式

@override
  Future<void> mapIsReady(bool isReady) async {
 
     
      LocationService locationService = LocationService(
        context: context,
        update_drivers: update_drivers,
        nearbyAvailableDriverKeysLoaded: nearbyAvailableDriverKeysLoaded,
        removeDriverIcon: removeDriverIcon,
        removeDriverKey: removeDriverKey,
        initPosition: initPosition,
        loadGeoFire: loadGeoFire,
      );

// calling the locatePosition method
      await locationService.locatePosition();
    }
  }
m2xkgtsf

m2xkgtsf1#

当然,即使在Dart中使用单例构造函数时,也可以传递参数。
这里我在构造函数中添加了位置参数,但是您也可以在其中添加命名参数。
下面是一个例子:

class LocationService {
  late BuildContext context;
  late Position position;
  late GeoPoint? initPosition;
  late bool loadGeoFire = false;
  late Function update_drivers;
  late bool nearbyAvailableDriverKeysLoaded;
  late bool removeDriverIcon;
  late String? removeDriverKey;

  static final LocationService _instance = LocationService._internal();
  LocationService._internal();

  factory LocationService(
    BuildContext context,
    Function update_drivers,
    bool nearbyAvailableDriverKeysLoaded,
    bool removeDriverIcon,
    String removeDriverKey,
    GeoPoint initPosition,
    bool loadGeoFire,
    Position position,
  ) {
    _instance.context = context;
    _instance.update_drivers = update_drivers;
    _instance.nearbyAvailableDriverKeysLoaded = nearbyAvailableDriverKeysLoaded;
    _instance.removeDriverIcon = removeDriverIcon;
    _instance.removeDriverKey = removeDriverKey;
    _instance.initPosition = initPosition;
    _instance.loadGeoFire = loadGeoFire;
    _instance.position = position;
    return _instance;
  }
}
4bbkushb

4bbkushb2#

我是这样做的

class LocationService {
  static LocationService? _locationService;

  final BuildContext context;
  final Function update_drivers;
  final bool nearbyAvailableDriverKeysLoaded;
  final bool removeDriverIcon;
  final String? removeDriverKey;
  GeoPoint? initPosition;
  bool loadGeoFire;


  late Position position;

  LocationService._({
    required this.context,
    required this.update_drivers,
    required this.nearbyAvailableDriverKeysLoaded,
    required this.removeDriverIcon,
    required this.removeDriverKey,
    required this.initPosition,
    required this.loadGeoFire,
  });

  factory LocationService({
    required BuildContext context,
    required Function update_drivers,
    required bool nearbyAvailableDriverKeysLoaded,
    required bool removeDriverIcon,
    required String? removeDriverKey,
    required GeoPoint? initPosition,
    required bool loadGeoFire,
  }) {
    _locationService ??= LocationService._(
      context: context,
      update_drivers: update_drivers,
      nearbyAvailableDriverKeysLoaded: nearbyAvailableDriverKeysLoaded,
      removeDriverIcon: removeDriverIcon,
      removeDriverKey: removeDriverKey,
      initPosition: initPosition,
      loadGeoFire: loadGeoFire,
    );
    return _locationService!;
  }

相关问题