You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter iOS配置flutter_local_notifications时参数标签错误求助

解决flutter_local_notifications iOS配置的参数标签错误问题

这个错误是Flutter SDK版本与示例代码版本不匹配导致的——不同版本的Flutter生成的GeneratedPluginRegistrant类中,register方法的参数标签不一样,要么是with:,要么是withRegistry:。

解决步骤:

  1. 打开iOS项目中的GeneratedPluginRegistrant.swift文件(路径:ios/Runner/GeneratedPluginRegistrant.swift)
  2. 找到register静态方法的定义,确认它的参数标签是with:还是withRegistry:
  3. 将AppDelegate.swift中所有调用GeneratedPluginRegistrant.register的地方,统一改成和文件里一致的参数标签

示例修正代码:

如果你的GeneratedPluginRegistrant里是withRegistry::

import UIKit
import Flutter
import Firebase
import flutter_local_notifications

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
        GeneratedPluginRegistrant.register(withRegistry: registry)
    }
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self
    }
    GeneratedPluginRegistrant.register(withRegistry: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

如果你的GeneratedPluginRegistrant里是with::

import UIKit
import Flutter
import Firebase
import flutter_local_notifications

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
  override func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  ) -> Bool {
    FirebaseApp.configure()
    FlutterLocalNotificationsPlugin.setPluginRegistrantCallback { (registry) in
        GeneratedPluginRegistrant.register(with: registry)
    }
    if #available(iOS 10.0, *) {
        UNUserNotificationCenter.current().delegate = self
    }
    GeneratedPluginRegistrant.register(with: self)
    return super.application(application, didFinishLaunchingWithOptions: launchOptions)
  }
}

额外说明:

  • 不要盲目照搬示例代码,示例代码的版本可能和你当前使用的Flutter SDK版本不一致
  • UNUserNotificationCenter.current().delegate = self不需要加as? UNUserNotificationCenterDelegate,因为FlutterAppDelegate已经默认实现了这个协议

内容的提问来源于stack exchange,提问作者mzkaoq

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.08 21:32:48