Flutter项目编译报错:Unresolved reference: createNotificationChannel
解决Task :location:compileDebugKotlin FAILED(Unresolved reference: createNotificationChannel)问题
问题根源
这段报错代码里,错误地用NotificationManagerCompat实例调用createNotificationChannel方法,但实际上这个方法是Android原生NotificationManager类的成员,NotificationManagerCompat并没有提供该方法,因此编译器报“Unresolved reference”错误。
具体解决思路
直接修改插件源码修复问题
找到报错文件C:\Users\DELL\Downloads\flutter_windows_2.10.2-stable\flutter\.pub-cache\hosted\pub.dartlang.org\location-4.4.0\android\src\main\java\com\lyokone\location\FlutterLocationService.kt,将NotificationManagerCompat替换为原生NotificationManager,修改后的代码如下:private fun updateChannel(channelName: String) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val channel = NotificationChannel( channelId, channelName, NotificationManager.IMPORTANCE_NONE ).apply { lockscreenVisibility = Notification.VISIBILITY_PRIVATE } notificationManager.createNotificationChannel(channel) } }保存修改后重新编译项目即可。
升级location插件版本
这个bug在location插件的后续版本中已被官方修复,直接在项目pubspec.yaml里把location插件版本更新到4.4.0以上的稳定版本,比如:dependencies: location: ^5.0.0 # 或更高兼容版本执行
flutter pub get拉取新依赖后重新编译项目。清理缓存后重试
如果修改代码或升级插件后仍有异常,可执行以下命令清理Flutter缓存,再重新构建:flutter clean flutter pub get
内容的提问来源于stack exchange,提问作者Praveesh Ramroop
相关产品推荐
相关产品推荐

