Flutter Web Firebase Auth编译错误:参数类型不匹配求助
错误详情
编译时触发如下错误:
/C:/Users/AppData/Local/Pub/Cache/hosted/pub.dev/firebase_auth_web-5.2.2/lib/firebase_auth_web.dart:92:45: Error: The argument type 'Future Function()' can't be assigned to the parameter type 'Future Function(App)?'.
'Future' is from 'dart:async'.
'App' is from 'package:firebase_core_web/src/interop/app.dart' ('/C:/Users/Grace%20Computer/AppData/Local/Pub/Cache/hosted/pub.dev/firebase_core_web-2.2.2/lib/src/interop/app.dart').
FirebaseCoreWeb.registerService('auth', () async {
^
Failed to compile application.
问题根源
这个错误由Firebase依赖版本不兼容导致:
firebase_auth包已默认包含Web平台实现,单独引入firebase_auth_web会引发版本冲突;- 未锁定
firebase_core和firebase_auth版本,pub自动拉取的firebase_auth_web:5.2.2与firebase_core_web:2.2.2参数定义不匹配,出现回调函数参数类型不兼容问题。
解决方案
1. 调整pubspec.yaml依赖配置
打开pubspec.yaml,移除单独的firebase_auth_web依赖,并为firebase_core和firebase_auth指定适配你SDK版本(>=2.19.2 <3.0.0)的兼容版本:
dependencies: flutter: sdk: flutter chat_bubbles: ^1.4.0 # 建议指定版本,避免依赖漂移 firebase_core: ^2.15.0 cupertino_icons: ^1.0.2 firebase_auth: ^4.10.0
2. 更新依赖并清理缓存
在终端执行以下命令:
flutter clean flutter pub get
3. 代码验证
你的main.dart初始化代码无需修改,确认web/index.html中Firebase凭证配置正确即可。
完成以上步骤后,重新编译项目即可解决参数类型不匹配问题。
内容的提问来源于stack exchange,提问作者prabinlamsal19

