解决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
相关产品推荐
相关产品推荐

