使用youtube_player_flutter包遇PlatformException,视频无法播放求助
问题描述
集成youtube_player_flutter包后,在iPhone模拟器中尝试播放视频时,界面始终停留在CircularProgressIndicator,同时触发如下PlatformException:
Unhandled Exception: PlatformException(unregistered_view_type, A UIKitView widget is trying to create a PlatformView with an unregistered type: < com.pichillilorenzo/flutter_inappwebview >, If you are the author of the PlatformView, make sure
registerViewFactoryis invoked. If you are not the author of the PlatformView, make sure to callGeneratedPluginRegistrant.register., null)
已尝试在info.plist中添加io.flutter.embedded_views_preview和NSAllowsArbitraryLoads,但问题未解决。
解决方案
以下是针对该问题的分步排查和解决方法:
重新安装并校验依赖
youtube_player_flutter内部依赖flutter_inappwebview,依赖版本不兼容或安装不完整会导致PlatformView注册失败:- 执行
flutter clean清理项目缓存 - 删除iOS目录下的
Podfile.lock文件 - 执行
flutter pub get重新拉取依赖 - 进入iOS目录,执行
pod install --repo-update更新Pod仓库并完成安装
- 执行
完善iOS项目配置
除已添加的两项配置,还需确认:- info.plist中
io.flutter.embedded_views_preview的值设为YES NSAppTransportSecurity字典下,NSAllowsArbitraryLoads设为YES(测试场景可用,上线建议配置特定域名的例外规则)- iOS项目的部署目标(Deployment Target)不低于iOS 11.0(flutter_inappwebview最低支持版本)
- info.plist中
重置模拟器状态
模拟器缓存或异常状态可能引发问题:- 关闭当前模拟器,从Xcode中重新启动
- 执行模拟器的「Erase All Content and Settings」操作,重置模拟器环境
指定兼容的依赖版本
若上述步骤无效,可在pubspec.yaml中直接指定与youtube_player_flutter兼容的flutter_inappwebview稳定版本,再重新执行依赖安装流程。
内容的提问来源于stack exchange,提问作者Raymond Idu

