我开发 flutter Web 应用程序已经有几年了,没有出现任何问题,直到我决定将其重构为原生 Macos 应用程序。
以下是我遇到的问题以及我如何解决其中大部分问题的总结。旅程还远未结束。
开箱即用,无法找到 pod 输出文件。修复方法是导入 AppInfo.xcconfig 中的文件。
我在构建时遇到这样的标头问题:
Pods/GoogleDataTransport/GoogleDataTransport/GDTCCTLibrary/Protogen/nanopb/compliance.nanopb.h:22:
Pods/nanopb/pb_decode.h:9:10: error: 'pb.h' file not found with <angled> include; use "quotes" instead
为了解决这个问题,我需要在每次构建之前运行此脚本:
pod deintegrate
rm -rf Pods
rm Podfile.lock
rm -rf ~/Library/Caches/CocoaPods
rm -rf ~/.cocoapods/repos
pod setup
pod install --repo-update
没有它我就无法连续构建两次。然后我可以构建,但 flutter run 有 2 个错误并产生黑屏。
我还需要运行预构建和构建后脚本来修复 Runner 目标构建阶段中的标头。
这是我的后期构建脚本,它们很相似
#!/bin/sh
# Paths to nanopb headers
NANOPB_HEADERS=(
"${SRCROOT}/Pods/nanopb/pb_common.h"
"${SRCROOT}/Pods/nanopb/pb_decode.h"
"${SRCROOT}/Pods/nanopb/pb_encode.h"
"${SRCROOT}/Pods/nanopb/pb.h"
)
# Paths to PromisesObjC headers
PROMISES_HEADERS=(
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+All.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Always.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Any.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Async.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Await.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Catch.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Delay.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Do.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Race.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Recover.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Reduce.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Retry.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Testing.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Then.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Timeout.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Validate.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise+Wrap.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromise.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromises.h"
"${SRCROOT}/Pods/PromisesObjC/Sources/FBLPromises/include/FBLPromiseError.h"
)
# Replace double-quoted includes with angle-bracketed includes in nanopb headers
for HEADER in "${NANOPB_HEADERS[@]}"; do
if [ -f "$HEADER" ]; then
sed -i '' 's/#include "\([^"]*\)"/#include <\1>/g' "$HEADER"
fi
done
# Replace double-quoted includes with angle-bracketed includes in PromisesObjC headers
for HEADER in "${PROMISES_HEADERS[@]}"; do
if [ -f "$HEADER" ]; then
sed -i '' 's/#include "\([^"]*\)"/#include <\1>/g' "$HEADER"
fi
done
即使付出了所有这些努力,Xcode 仍报告 Pods 在构建后始终需要更新。我还看到 MainFlutterWindow.swift 抱怨找不到 FlutterMacOS 框架,尽管 AppDelegate.swift 需要它并且可以看到它。
我花了一周的时间试图解决这些问题,除非我能找到解决方案,否则我打算放弃苹果开发。 Cocopods 似乎太脆弱而无法使用。
Mac m2 版本 14.6,Flutter 3.22.3。
请尝试将您的 cocoapods 升级到最新版本,例如1.10 或 1.15,或更新到预发布版本,如这个 SO 问题
中所示