0

I haver a flutter project in which I have created a widget which I need to update if data is changed in app, hence I am using flutter method channel to call a method defined in Swiftui appDelegate file which will reload timeline of widget, however the Issue I am facing is that the first time the app launches and I invoke this method it did not work but later when I invoke this method it works.

appDelegate.swift:

Override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
      let controller = window.rootViewController as! FlutterViewController
      let widgetReloadChannel = FlutterMethodChannel(name: "widget_reload_channel", binaryMessenger: controller.binaryMessenger)
      widgetReloadChannel.setMethodCallHandler { (call: FlutterMethodCall, result: @escaping FlutterResult) in
        if call.method == "reloadWidgetTimeline" {
            if #available(iOS 14.0, *) {
//                WidgetCenter.shared.reloadAllTimelines()
                WidgetCenter.shared.reloadTimelines(ofKind: "PrayerWidget")
                print("-- RELOAD DONE")
            }
          result(nil)
        } else {
          result(FlutterMethodNotImplemented)
        }
      }
      
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }

.dart file:

const methodChannel = MethodChannel("widget_reload_channel");
  try {
    await methodChannel.invokeMethod("reloadWidgetTimeline");
    debugPrint("-- methodChannel invoked");
  } catch (e) {
    debugPrint("-- methodChannelError $e");
  }

0 Answers0