You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加firebase_auth后Flutter iOS应用构建失败求助

Flutter添加firebase_auth后iOS端Swift编译失败问题

移除firebase_auth包后,Flutter项目可正常在iOS端运行,但添加该包后出现多处Swift编译错误,提示"Expected declaration"。

编译错误信息

Swift Compiler Error (Xcode): Expected declaration
/Users/tecspine/Flutter%20Projects/plus-one-mobile/ios/Pods/FirebaseAuth/FirebaseAuth/Sources/Swift/Auth/Auth.swift:292:2

Swift Compiler Error (Xcode): Expected declaration
/Users/tecspine/Flutter%20Projects/plus-one-mobile/ios/Pods/FirebaseAuth/FirebaseAuth/Sources/Swift/AuthProvider/OAuthProvider.swift:42:2

Swift Compiler Error (Xcode): Expected declaration
/Users/tecspine/Flutter%20Projects/plus-one-mobile/ios/Pods/FirebaseAuth/FirebaseAuth/Sources/Swift/AuthProvider/OAuthProvider.swift:68:2

Swift Compiler Error (Xcode): Expected declaration
/Users/tecspine/Flutter%20Projects/plus-one-mobile/ios/Pods/FirebaseAuth/FirebaseAuth/Sources/Swift/AuthProvider/OAuthProvider.swift:144:2

Swift Compiler Error (Xcode): Expected declaration
/Users/tecspine/Flutter%20Projects/plus-one-mobile/ios/Pods/FirebaseAuth/FirebaseAuth/Sources/Swift/AuthProvider/OAuthProvider.swift:181:2

Swift Compiler Error (Xcode): Expected declaration
/Users/tecspine/Flutter%20Projects/plus-one-mobile/ios/Pods/FirebaseAuth/FirebaseAuth/Sources/Swift/AuthProvider/OAuthProvider.swift:211:2

开发环境信息

flutter doctor -v
[✓] Flutter (Channel stable, 3.27.1, on macOS 12.7.6 21H1320 darwin-x64, locale en-PK)
    • Flutter version 3.27.1 on channel stable at /Users/tecspine/Development/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 17025dd882 (3 weeks ago), 2024-12-17 03:23:09 +0900
    • Engine revision cb4b5fff73
    • Dart version 3.6.0
    • DevTools version 2.40.2

[✓] Android toolchain - develop for Android devices (Android SDK version 35.0.0)
    • Android SDK at /Users/tecspine/Library/Android/sdk
    • Platform android-35, build-tools 35.0.0
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)
    • All Android licenses accepted.

[!] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    ! Flutter recommends a minimum Xcode version of 15.
      Download the latest version or update via the Mac App Store.
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 17.0.10+0-17.0.10b1087.21-11609105)

[✓] VS Code (version 1.96.1)
    • VS Code at /Users/tecspine/Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.102.0

[✓] Connected device (3 available)
    • iPhone 14 Pro (mobile) • F6314460-474D-465E-930D-305B0E56BF9D • ios            • com.apple.CoreSimulator.SimRuntime.iOS-16-2 (simulator)
    • macOS (desktop)        • macos                                • darwin-x64     • macOS 12.7.6 21H1320 darwin-x64
    • Chrome (web)           • chrome                               • web-javascript • Google Chrome 131.0.6778.205

[✓] Network resources
    • All expected network resources are available.

已尝试的解决方法

  • 删除Podfile、Podfile.lock及Pods目录
  • 清除pub缓存
  • 清除Xcode构建缓存
  • 更新Pods

解决方案

1. 降级firebase_auth版本适配Xcode 14.2

最新版firebase_auth可能使用了Xcode 15才支持的Swift语法,在pubspec.yaml中指定兼容Xcode 14的版本:

firebase_auth: ^4.17.8

执行以下命令同步依赖:

flutter pub get
cd ios
pod install

2. 升级Xcode到15+(推荐)

Flutter 3.27.1官方要求最低Xcode版本为15,Xcode 14.2存在语法兼容性问题。直接通过Mac App Store或Apple开发者网站更新到最新稳定版,更新后重新构建项目。

3. 调整Swift版本设置

在Xcode中打开iOS项目,进入Pods -> FirebaseAuth -> Build Settings,找到Swift Language Version,设置为Xcode 14.2支持的最高版本(Swift 5.7)。

4. 完整清理重建

若上述方法无效,执行彻底清理流程:

flutter clean
rm -rf ios/Pods ios/Podfile.lock ios/Runner.xcworkspace
flutter pub get
cd ios
pod deintegrate
pod install

内容的提问来源于stack exchange,提问作者Noman Khan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 01:54:51