iOS端Shared_preferences抛出PlatformException通道连接异常求助
iOS下Flutter应用启动时出现PlatformException(channel-error)问题
我见过很多关于这个错误的帖子,但大多和Firebase相关。这是个极简应用——无Firebase、无网络,本质是个针对特定问题的计算器。Android上运行正常,但iOS启动失败。
Pubspec.yaml依赖
environment: sdk: '>=3.0.6 <4.0.0' dependencies: flutter: sdk: flutter shared_preferences: ^2.2.1 async: ^2.11.0 dev_dependencies: flutter_test: sdk: flutter flutter_lints: ^2.0.3 flutter: uses-material-design: true
我只添加了两个库:shared_preferences(用于存储设置)和async(用于FutureGroup)。
main.dart的main函数
void main() { WidgetsFlutterBinding.ensureInitialized(); SharedPrefs().init().then((void _) { SpecificModel.load(); runApp(MyApp()); }); }
错误发生在SharedPrefs().init()这一行(原本将main设为async并使用await,后来尝试各种方法改成了.then)。
错误输出
Flutter命令行运行输出
$ flutter run -d B9B5C8EE-0597-4780-8AB9-86F778472008 Launching lib/main.dart on iPhone 14 Pro in debug mode... Running pod install... 520ms Running Xcode build... └─Compiling, linking and signing... 2,825ms Xcode build done. 17.5s [VERBOSE-2:FlutterDarwinContextMetalImpeller.mm(35)] 使用Impeller渲染后端。 [VERBOSE-2:dart_vm_initializer.cc(41)] 未处理的异常:PlatformException(channel-error, 无法建立通道连接。, null, null) Syncing files to device iPhone 14 Pro... 76ms
Xcode直接运行错误
直接在Xcode中运行完全无法链接,提示Framework not found shared_preferences_foundation。但通过Flutter命令行构建正常:
flutter build ios Changing current working directory to: /Users/blahblah/project Building com.company.project for device (ios-release)... Automatically signing iOS for device deployment using specified development team in Xcode project: ########## Running pod install... 512ms Running Xcode build... └─Compiling, linking and signing... 4.2s Xcode build done. 32.9s Built /Users/blahblah/project/build/ios/iphoneos/Runner.app.
已尝试的解决方法
flutter doctor显示一切正常,flutter pub outdated显示所有直接和间接依赖均为最新版本- 多次执行
flutter clean、rm -rf ios/Pods、rm podFile.lock、flutter pub get流程 - 清理pod缓存、取消集成、重建Flutter缓存后执行pod install
- 新建Flutter项目复制现有代码,能正常运行一段时间,但之后错误复现,因此确定问题不在代码而在环境
补充信息
pubspec.lock中shared_preferences相关部分
shared_preferences: dependency: "direct main" description: name: shared_preferences sha256: b7f41bad7e521d205998772545de63ff4e6c97714775902c199353f8bf1511ac url: "https://pub.dev" source: hosted version: "2.2.1" shared_preferences_android: dependency: transitive description: name: shared_preferences_android sha256: fe8401ec5b6dcd739a0fe9588802069e608c3fdbfd3c3c93e546cf2f90438076 url: "https://pub.dev" source: hosted version: "2.2.0" shared_preferences_foundation: dependency: transitive description: name: shared_preferences_foundation sha256: d29753996d8eb8f7619a1f13df6ce65e34bc107bef6330739ed76f18b22310ef url: "https://pub.dev" source: hosted version: "2.3.3" shared_preferences_linux: dependency: transitive description: name: shared_preferences_linux sha256: "71d6806d1449b0a9d4e85e0c7a917771e672a3d5dc61149cc9fac871115018e1" url: "https://pub.dev" source: hosted version: "2.3.0" shared_preferences_platform_interface: dependency: transitive description: name: shared_preferences_platform_interface sha256: "23b052f17a25b90ff2b61aad4cc962154da76fb62848a9ce088efe30d7c50ab1" url: "https://pub.dev" source: hosted version: "2.3.0" shared_preferences_web: dependency: transitive description: name: shared_preferences_web sha256: "7347b194fb0bbeb4058e6a4e87ee70350b6b2b90f8ac5f8bd5b3a01548f6d33a" url: "https://pub.dev" source: hosted version: "2.2.0" shared_preferences_windows: dependency: transitive description: name: shared_preferences_windows sha256: f95e6a43162bce43c9c3405f3eb6f39e5b5d11f65fab19196cf8225e2777624d url: "https://pub.dev" source: hosted version: "2.3.0"
Podfile.lock内容
PODS: - Flutter (1.0.0) - shared_preferences_foundation (0.0.1): - Flutter - FlutterMacOS DEPENDENCIES: - Flutter (from `Flutter`) - shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`) EXTERNAL SOURCES: Flutter: :path: Flutter shared_preferences_foundation: :path: ".symlinks/plugins/shared_preferences_foundation/darwin" SPEC CHECKSUMS: Flutter: f04841e97a9d0b0a8025694d0796dd46242b2854 shared_preferences_foundation: 5b919d13b803cadd15ed2dc053125c68730e5126 PODFILE CHECKSUM: 6b9eb94e9f98a329f2ef624b852a6e42d090af2b COCOAPODS: 1.12.1
.symlinks/plugins/shared_preferences_foundation/darwin路径下内容
~/Documents/repo/personal/project_bad/ios/.symlinks/plugins/shared_preferences_foundation/darwin $ ls -l total 8 drwxr-xr-x 4 gbryant staff 128 Aug 15 12:29 Classes drwxr-xr-x 3 gbryant staff 96 Aug 15 12:29 Tests -rw-r--r-- 1 gbryant staff 1269 Aug 15 12:29 shared_preferences_foundation.podspec
调用栈

内容的提问来源于stack exchange,提问作者gbryant
相关产品推荐
相关产品推荐

