我有一个适用于 Android 和 iOS 的天气应用程序。android 版本有一个主屏幕小部件,它使用 Home_widget 和 WorkManager 插件在后台更新,我正在尝试在 iOS 上实现相同的功能。我当然不是专家,但了解当工作管理器回调过程在 iOS 上启动时,会创建一个 dart 隔离。这意味着在flutter主进程中注册的插件不可用,我需要利用WorkmanagerPlugin的setPluginRegistrantCallback函数在隔离中注册所需的插件。
这是我的 AppDelegate 类:
import UIKit
import Flutter
import workmanager
import home_widget
@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
override func application(
_ application: UIApplication,
didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
) -> Bool {
GeneratedPluginRegistrant.register(with: self)
UIApplication.shared.setMinimumBackgroundFetchInterval(TimeInterval(60*15))
WorkmanagerPlugin.setPluginRegistrantCallback { registry in
GeneratedPluginRegistrant.register(with: registry)
}
return super.application(application, didFinishLaunchingWithOptions: launchOptions)
}
}
该行
GeneratedPluginRegistrant.register(with: registry)
应该注册所有插件并且确实如此。
但是,我需要 http 插件(https://pub.dev/packages/http)并且它没有在主进程中注册,所以没有在隔离中注册。我知道我可以使用类似以下内容来注册特定插件,但我不知道 http 插件的完整包名,或者这是否可行。
HomeWidgetPlugin.register(with: registry.registrar(forPlugin: "es.antonborri.home_widget.HomeWidgetPlugin") as! FlutterPluginRegistrar)
这是我的 GeneratedPluginRegistrant.java 显示 http 插件未注册:
public final class GeneratedPluginRegistrant {
public static void registerWith(@NonNull FlutterEngine flutterEngine) {
ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(flutterEngine);
flutterEngine.getPlugins().add(new com.jrai.flutter_keyboard_visibility.FlutterKeyboardVisibilityPlugin());
com.aloisdeniel.geocoder.GeocoderPlugin.registerWith(shimPluginRegistry.registrarFor("com.aloisdeniel.geocoder.GeocoderPlugin"));
flutterEngine.getPlugins().add(new com.baseflow.geocoding.GeocodingPlugin());
com.ggichure.github.hexcolor.HexcolorPlugin.registerWith(shimPluginRegistry.registrarFor("com.ggichure.github.hexcolor.HexcolorPlugin"));
flutterEngine.getPlugins().add(new es.antonborri.home_widget.HomeWidgetPlugin());
flutterEngine.getPlugins().add(new com.lyokone.location.LocationPlugin());
flutterEngine.getPlugins().add(new dev.fluttercommunity.plus.packageinfo.PackageInfoPlugin());
flutterEngine.getPlugins().add(new io.flutter.plugins.pathprovider.PathProviderPlugin());
flutterEngine.getPlugins().add(new com.baseflow.permissionhandler.PermissionHandlerPlugin());
flutterEngine.getPlugins().add(new io.flutter.plugins.urllauncher.UrlLauncherPlugin());
flutterEngine.getPlugins().add(new io.flutter.plugins.videoplayer.VideoPlayerPlugin());
flutterEngine.getPlugins().add(new creativemaybeno.wakelock.WakelockPlugin());
flutterEngine.getPlugins().add(new io.flutter.plugins.webviewflutter.WebViewFlutterPlugin());
flutterEngine.getPlugins().add(new be.tramckrijte.workmanager.WorkmanagerPlugin());
}
}
很长一段时间,但我的问题是如何在隔离中使用 http?
(这方面的一些背景。我收到以下错误:
Unhandled Exception: MissingPluginException(No implementation found for method registerBackgroundCallback on channel home_widget)
单步执行我的代码时,此异常发生在我尝试使用来自 api 的 http 插件获取数据的行。因此,通过大量的挖掘和阅读,我认为这是由于 http 没有在隔离中注册 - 但我可能是错的?!)