问题是 geolocator_android 版本 6 中的默认方法通道实现 3.1. 已被平台特定的实现所取代。但是,由于任务在没有 Flutter 引擎的情况下在单独的隔离中运行,因此特定于平台的实现(在本例中为 geolocator_android)没有注册到平台接口(@987654325@),导致 MissingPluginException 。
要使用 3.1.6 或更高版本,请务必在运行 executeTask 时注册特定于平台的实现。
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
if (defaultTargetPlatform == TargetPlatform.android) {
GeolocatorAndroid.registerWith();
} else if (defaultTargetPlatform == TargetPlatform.iOS || defaultTargetPlatform == TargetPlatform.macOS) {
GeolocatorApple.registerWith();
} else if (defaultTargetPlatform == TargetPlatform.linux) {
GeolocatorLinux.registerWith();
}
await Geolocator.checkPermission();
await Geolocator.getCurrentPosition();
});
}
或者,如果你正在运行 Flutter 2.11+,你可以使用新的 DartPluginRegistrant.ensureInitialized() 方法来确保所有包都正确注册:
void callbackDispatcher() {
Workmanager().executeTask((task, inputData) async {
DartPluginRegistrant.ensureInitialized();
await Geolocator.checkPermission();
await Geolocator.getCurrentPosition();
});
}
更多信息可以在 和 找到。
© 版权声明
本站下载的源码均来自公开网络收集转发二次开发而来,
若侵犯了您的合法权益,请来信通知我们1413333033@qq.com,
我们会及时删除,给您带来的不便,我们深表歉意。
下载用户仅供学习交流,若使用商业用途,请购买正版授权,否则产生的一切后果将由下载用户自行承担,访问及下载者下载默认同意本站声明的免责申明,请合理使用切勿商用。
THE END
暂无评论内容