执行flutter build web时出现包冲突类/方法未找到构建错误
Flutter Web构建失败问题解决
问题
执行flutter build web时触发dart2js编译错误,无法完成构建,但flutter run main.dart可以正常运行。
错误日志
Compiling lib/main.dart for the Web... Target dart2js failed: ProcessException: Process exited abnormally with exit code 1: ../../../.pub-cache/hosted/pub.dev/firebase_auth_web-5.8.13/lib/src/interop/auth_interop.dart:26:10: Error: Type 'PromiseJsImpl' not found. external PromiseJsImpl<void> applyActionCode(AuthJsImpl auth, String oobCode); ^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/firebase_auth_web-5.8.13/lib/src/interop/auth_interop.dart:38:10: Error: Type 'PromiseJsImpl' not found. external PromiseJsImpl<ActionCodeInfo> checkActionCode( ^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/firebase_auth_web-5.8.13/lib/src/interop/auth_interop.dart:42:10: Error: Type 'PromiseJsImpl' not found. external PromiseJsImpl<void> confirmPasswordReset( ^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/firebase_auth_web-5.8.13/lib/src/interop/auth_interop.dart:55:10: Error: Type 'PromiseJsImpl' not found. external PromiseJsImpl<void> setPersistence( ^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/firebase_storage_web-3.6.22/lib/src/interop/storage.dart:154:7: Error: The method 'handleThenable' isn't defined for the class 'StorageReference'. 'StorageReference' is from 'package:firebase_storage_web/src/interop/storage.dart' ('../../../.pub-cache/hosted/pub.dev/firebase_storage_web-3.6.22/lib/src/interop/storage.dart'). handleThenable(storage_interop.list(jsObject, options?.jsObject)) ^^^^^^^^^^^^^^ ../../../.pub-cache/hosted/pub.dev/firebase_storage_web-3.6.22/lib/src/interop/storage.dart:169:7: Error: The method 'handleThenable' isn't defined for the class 'StorageReference'. 'StorageReference' is from 'package:firebase_storage_web/src/interop/storage.dart' ('../../../.pub-cache/hosted/pub.dev/firebase_storage_web-3.6.22/lib/src/interop/storage.dart'). handleThenable(storage_interop.listAll(jsObject)) ^^^^^^^^^^^^^^
项目pubspec.yaml配置
dependencies: flutter: sdk: flutter cupertino_icons: ^1.0.2 share_plus: ^10.1.0 flutter_azure_tts: 0.2.3 http: ^1.0.0 flutter_dotenv: ^5.1.0 audio_service: ^0.18.12 just_audio: ^0.9.42 just_audio_web: ^0.4.13 rxdart: ^0.27.7 flutter_secure_storage: ^9.2.4 async: ^2.11.0 synchronized: ^3.1.0+1 scroll_to_index: ^3.0.1 audioplayers: ^6.1.0 flutter_markdown: ^0.6.10 cloud_firestore: ^4.16.1 firebase_auth: 4.12.0 firebase_core: ^2.25.2 firebase_storage: ^11.6.5 firebase_ui_auth: ^1.7.0 firebase_ui_oauth_google: ^1.2.10 google_sign_in: ^6.2.1 shared_preferences: ^2.0.0 pref: null settings_ui: ^2.0.2 flutter_settings_screens: ^0.3.4 hive_flutter: ^1.0.0 printing: 5.12.0 pdf: ^3.10.0
解决方案
错误根源是Firebase相关依赖版本不兼容:firebase_auth、firebase_storage与其对应的web实现包(firebase_auth_web、firebase_storage_web)版本不匹配,导致dart2js编译时无法找到正确的类型和方法。开发环境下的dartdevc编译器对这类不兼容问题容忍度更高,因此flutter run能正常执行。
解决步骤:
- 更新Firebase依赖到兼容版本:修改
pubspec.yaml中的Firebase相关依赖,确保版本互相适配。建议使用以下版本(或最新兼容版本):cloud_firestore: ^4.16.1 firebase_auth: ^4.16.0 firebase_core: ^2.25.2 firebase_storage: ^11.6.7 - 清理并重新获取依赖:
flutter clean flutter pub get - 重新构建Web项目:
flutter build web
内容的提问来源于stack exchange,提问作者Ryan
相关产品推荐
相关产品推荐

