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

Flutter类型转换错误:List<Object?>无法转为PigeonUserDetails?

类型转换错误:List<Object?> 不是 PigeonUserDetails? 的子类型 修复方案

问题详情

出现类型转换错误:输入类型'List<Object?>'不是'PigeonUserDetails?'的子类型,已执行flutter pub upgrade firebase_auth和flutter pub upgrade firebase_core升级依赖,但错误仍存在。

相关代码

import 'package:chatty/common/entities/entities.dart';
import 'package:chatty/pages/frame/sign_in/index.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:google_sign_in/google_sign_in.dart';

import '../../../common/routes/routes.dart';

class SignInController extends GetxController{
  SignInController();
  final state = SignInState();

  final GoogleSignIn _googleSignIn = GoogleSignIn(
      scopes: [
        'openid'
      ]
  ); 

  Future<void> handleSignIn(String type) async {
    try{ 
      if(type == "phone number"){
        if (kDebugMode) {
          print("Bạn đang đăng nhập với SĐT");
        }
      }else if(type == "google"){
        var user = await _googleSignIn.signIn();
        if (user != null){

          final _gAuthentication = await user.authentication;
          final _credential = GoogleAuthProvider.credential(
            idToken: _gAuthentication.idToken,
            accessToken: _gAuthentication.accessToken
          );
          
          await FirebaseAuth.instance.signInWithCredential(_credential);

          String? displayName = user.displayName;
          String email = user.email;
          String id = user.id;
          String photoUrl = user.photoUrl ?? "assets/icons/google.png";
          LoginRequestEntity loginPanelListRequestEntity = LoginRequestEntity();
          loginPanelListRequestEntity.avatar = photoUrl;
          loginPanelListRequestEntity.name = displayName;
          loginPanelListRequestEntity.email = email;
          loginPanelListRequestEntity.open_id = id;
          loginPanelListRequestEntity.type = 2;
          asyncPostAllData();
        }
      }else{
        if (kDebugMode) {
          print("Đăng nhập không xác định");
        }
      }
    }catch(e){
      if (kDebugMode) {
        print('Đăng nhập $e bị lỗi');
      }
    }
  }
  asyncPostAllData(){
    print("Chuyển đến trang tin nhắn");
    Get.offAllNamed(AppRoutes.Message);
  }
}

当前依赖版本

  • google_sign_in: ^6.1.5
  • firebase_core: ^3.6.0
  • firebase_messaging: ^15.1.3
  • firebase_in_app_messaging: ^0.8.0+8
  • firebase_auth: ^5.3.1
  • cloud_firestore: ^5.4.4
  • firebase_storage: ^12.3.4

修复步骤

1. 解决依赖版本不兼容问题

Firebase Auth 5.x+ 和 google_sign_in 6.1.5 存在版本适配冲突,Pigeon作为Firebase内部跨平台通信工具,版本不匹配会直接触发类型转换错误。

操作:

  • 修改pubspec.yaml中的google_sign_in版本,升级到兼容版本:
    google_sign_in: ^6.2.1 # 或更高的兼容版本
    
  • 执行命令更新依赖并清理缓存:
    flutter pub get
    flutter clean
    flutter pub cache repair
    

2. 彻底清理项目构建缓存

旧缓存可能残留不兼容的Pigeon生成代码,导致错误持续存在。

操作:

flutter clean
rm -rf ios/Pods ios/Podfile.lock android/.gradle
flutter pub get
cd ios && pod install --repo-update && cd ..

完成后重启IDE,重新运行项目。

3. 检查Firebase初始化流程

确保main.dart中Firebase初始化顺序正确:

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(const MyApp());
}

4. 替换Google登录实现(备选方案)

如果上述方法无效,直接使用Firebase Auth原生的Google登录方法,绕开google_sign_in插件的潜在问题:

// 替换原handleSignIn中的Google登录逻辑
else if(type == "google"){
  final GoogleAuthProvider googleProvider = GoogleAuthProvider();
  googleProvider.addScope('openid');

  final UserCredential userCredential = await FirebaseAuth.instance.signInWithPopup(googleProvider);
  final User? user = userCredential.user;

  if (user != null) {
    String? displayName = user.displayName;
    String email = user.email!;
    String id = user.uid;
    String photoUrl = user.photoURL ?? "assets/icons/google.png";
    LoginRequestEntity loginPanelListRequestEntity = LoginRequestEntity();
    loginPanelListRequestEntity.avatar = photoUrl;
    loginPanelListRequestEntity.name = displayName;
    loginPanelListRequestEntity.email = email;
    loginPanelListRequestEntity.open_id = id;
    loginPanelListRequestEntity.type = 2;
    asyncPostAllData();
  }
}

内容的提问来源于stack exchange,提问作者0935_Lường Tuấn Anh

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.16 22:13:22