我试着创建一个有工厂方法的类,并把它作为一个单例类来使用。然而,我的类需要一些构造函数,这样做我总是不能正确地编写它
有可能传递构造函数并使用工厂方法吗?我还没能这么做。这是类
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();
}
}
2条答案
按热度按时间m2xkgtsf1#
当然,即使在Dart中使用单例构造函数时,也可以传递参数。
这里我在构造函数中添加了位置参数,但是您也可以在其中添加命名参数。
下面是一个例子:
4bbkushb2#
我是这样做的