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

执行Flutter run命令时遇Xcode语义错误及废弃警告,求解决方案

解决flutter_local_notifications编译错误的方案

核心原因

你遇到的问题是因为使用的flutter_local_notifications 9.4.0版本过于老旧,和当前Xcode/iOS SDK的API规范不兼容:

  • UNNotification的userInfo属性访问方式在新版本SDK中需通过request.content获取,旧插件代码写法过时
  • UILocalNotification是iOS 10之前的API,早已被废弃,新SDK会抛出警告

推荐解决方案:更新插件到最新稳定版

这是最彻底的解决方式,步骤如下:

  1. 打开项目根目录的pubspec.yaml文件,找到flutter_local_notifications的依赖项,将版本号改为最新稳定版(比如^16.1.0,也可直接写flutter_local_notifications: ^latest_version,执行命令会自动拉取最新适配版本)
  2. 在终端执行:
    flutter pub upgrade flutter_local_notifications
    flutter clean
    flutter pub get
    
  3. 重新运行flutter run即可

临时修复方案(不推荐,仅当无法更新插件时使用)

如果暂时不能升级插件,可手动修改插件源码:

  1. 找到报错的文件路径:/Users/martyn/FlutterDev/flutter/.pubcache/hosted/pub.dartlang.org/flutter_local_notifications-9.4.0/ios/Classes/FlutterLocalNotificationsPlugin.m
  2. 定位到第156行,将原来的notification.userInfo相关代码修改为:
    notification.request.content.userInfo
    
    同时检查数组下标是否为整数,修正非整数下标的写法
  3. 对于UILocalNotification的废弃警告,可暂时忽略(若必须处理,需修改插件代码改用UNNotificationRequest,但工作量较大,不如直接升级插件)

辅助清理步骤

如果上述操作后仍有问题,可执行以下清理操作:

  1. 清除pub缓存:
    flutter pub cache clean
    
  2. 打开Xcode,按Command + Shift + K清理构建文件夹,关闭Xcode后再重新运行flutter run

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 02:30:48