I have a weather app that works on Android and iOS. The android version has a home screen widget that updates in the background using the Home_widget and WorkManager plugin's, and I'm trying to implement the same on iOS. I am certainly no expert, but understand that when the work manager callback process starts on iOS, a dart isolate is created. This means that plugin's registered with flutter main process are not available and I need to make use of WorkmanagerPlugin's setPluginRegistrantCallback function to register the required plugin's in the isolate.

Here is my AppDelegate class:

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)
  }
}

The line GeneratedPluginRegistrant.register(with: registry) should register all plugin's and it does.

However, I need the http plugin (https://pub.dev/packages/http) and it is not registered in the main process, so does not get registered in the isolate. I know I can use something like the following to register specific plugin's, but I do not know the full package name for the http plugin, or if this would even work.

HomeWidgetPlugin.register(with: registry.registrar(forPlugin: "es.antonborri.home_widget.HomeWidgetPlugin") as! FlutterPluginRegistrar)

Here is my GeneratedPluginRegistrant.java showing that the http plugin is not registered:

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());
  }
}

Long lead up, but my question is how do I use http in the isolate?

(Some background to this. I am getting the following error:

Unhandled Exception: MissingPluginException(No implementation found for method registerBackgroundCallback on channel home_widget)

When stepping through my code, this exception occurs on the line where I try to get data using the http plugin from an api. Hence, through a lot of digging & reading I think it is due to http not being registered in the isolate - but I could be wrong?!)

0

There are 0 best solutions below