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

解决Flutter中Instance member 'signIn'无法静态访问的错误

问题修复方案

错误Instance member 'signIn' can't be accessed using static access的核心原因是:你试图通过类名直接调用实例方法,但signIn是GoogleAuthApi类的非静态成员,必须先创建类的实例,或者将其改为静态方法才能通过类名调用。

同时注意原代码里的一个细节错误:GoogleSignIn的isSignedIn是异步方法,不是属性,需要用await调用isSignedIn(),而非直接判断isSignedIn == true。

方案一:将方法改为静态成员

修改GoogleAuthApi类,把_googleSignIn和signIn都改为静态:

class GoogleAuthApi {
  static final _googleSignIn = GoogleSignIn();

  static Future<GoogleSignInAccount?> signIn() async {
    if (await _googleSignIn.isSignedIn()) {
      return _googleSignIn.currentUser;
    } else {
      return await _googleSignIn.signIn();
    }
  }
}

调用时保持类名调用,但要补上方法括号:

final googleUser = await GoogleAuthApi.signIn();

方案二:创建类实例后调用方法

在sendNewUserEmail函数中先实例化GoogleAuthApi,再调用实例方法:

Future sendNewUserEmail() async {
  final googleAuth = GoogleAuthApi();
  final googleUser = await googleAuth.signIn();
  // 后续邮件发送代码不变...
}

同时记得修正GoogleAuthApi里的isSignedIn判断:

// 原错误写法
if (_googleSignIn.isSignedIn == true)
// 修正为
if (await _googleSignIn.isSignedIn())

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.30 21:22:45