执行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会抛出警告
推荐解决方案:更新插件到最新稳定版
这是最彻底的解决方式,步骤如下:
- 打开项目根目录的
pubspec.yaml文件,找到flutter_local_notifications的依赖项,将版本号改为最新稳定版(比如^16.1.0,也可直接写flutter_local_notifications: ^latest_version,执行命令会自动拉取最新适配版本) - 在终端执行:
flutter pub upgrade flutter_local_notifications flutter clean flutter pub get - 重新运行
flutter run即可
临时修复方案(不推荐,仅当无法更新插件时使用)
如果暂时不能升级插件,可手动修改插件源码:
- 找到报错的文件路径:
/Users/martyn/FlutterDev/flutter/.pubcache/hosted/pub.dartlang.org/flutter_local_notifications-9.4.0/ios/Classes/FlutterLocalNotificationsPlugin.m - 定位到第156行,将原来的
notification.userInfo相关代码修改为:
同时检查数组下标是否为整数,修正非整数下标的写法notification.request.content.userInfo - 对于
UILocalNotification的废弃警告,可暂时忽略(若必须处理,需修改插件代码改用UNNotificationRequest,但工作量较大,不如直接升级插件)
辅助清理步骤
如果上述操作后仍有问题,可执行以下清理操作:
- 清除pub缓存:
flutter pub cache clean - 打开Xcode,按
Command + Shift + K清理构建文件夹,关闭Xcode后再重新运行flutter run
内容的提问来源于stack exchange,提问作者Chet
相关产品推荐
相关产品推荐

