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

Flutter实现Firebase匿名登录时报NoSuchMethodError: call方法调用在null上

问题原因
  • 核心错误是类名与构造函数名不匹配:你定义的组件类名为ExcRoute,但构造函数却命名为const Exc(...),Dart不会将其识别为ExcRoute的默认构造函数,只会自动生成无参数的默认构造。你在landing_page.dart中实例化ExcRoute时传入的onSignIn参数没有被赋值给类的成员变量,导致onSignIn为null,调用时就触发了The method 'call' was called on null的报错。
  • 次要隐患:你使用的firebase_core: 0.5.1、firebase_auth: 0.18.2属于非常老旧的版本,部分API行为存在差异,也可能存在兼容问题。
修复方案
  1. 首先修正构造函数命名,将ExcRoute类的构造函数名改为和类名一致:
// 修正前
const Exc({Key key, @required this.onSignIn}) : super(key: key);

// 修正后
const ExcRoute({Key key, @required this.onSignIn}) : super(key: key);
  1. 补充判空逻辑避免后续异常,调用onSignIn前先判断是否为空:
Future<void> _signInAnonymously() async {
  try {
    final userCredentials = await FirebaseAuth.instance.signInAnonymously();
    // 增加判空逻辑
    if (onSignIn != null) {
      onSignIn(userCredentials.user);
    }
  } catch (e) {
    print(e.toString());
  }
}
  1. (可选)升级Firebase相关依赖到稳定兼容版本,适配空安全等特性,参考配置:
dependencies:
  firebase_core: ^2.15.0
  firebase_auth: ^4.10.0

升级后需要对应调整初始化和API调用的写法,新版本Firebase.initializeApp需传入对应项目的options配置参数。

内容的提问来源于stack exchange,提问作者Alex Gk

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.30 22:57:03