首页 > 解决方案 > Flutter: Instance member 'signInWithGoogle' can't be accessed using static access. (static_access_to_instance_member at )

问题描述

I try to lint for my Flutter project, I have a class API to log in and log out google account, Linter prefers to remove static before these methods (login with Google and sign out). I cannot call these functions in view. Here my code:

API.dart

class FBApi {
FBApi(this.firebaseUser);

  ...

  Future<FBApi> signInWithGoogle() async {
    final GoogleSignInAccount googleUser = await _googleSignIn.signIn();
    final GoogleSignInAuthentication googleAuth =
        await googleUser.authentication;

    ...
  }


Future<void> signOut() async {
    await _auth.signOut().then((_) {
      print('***** log out...what the hell?');
      _googleSignIn.disconnect();
      _googleSignIn.signOut();
      // Navigator.of(context).pushNamedAndRemoveUntil("/login", ModalRoute.withName("/home"));
    });
  }
}

Login.dart error above

Future<bool> _loginUser() async {
    final FBApi api = await FBApi.signInWithGoogle();---> error
    if (api != null) {
      return true;
    } else {
      return false;
    }
  }

Logout.dart

Future<void> _signOut() async {
    try {
      await FBApi.signOut();
    } catch (e) {
      print(e);
    }
  }

标签: firebasedartflutterfirebase-authenticationgoogle-cloud-firestore

解决方案


await FBApi.signInWithGoogle();---> error

应该

await FBApi().signInWithGoogle();

您首先需要创建一个实例()来调用实例方法。

或者,您可以更改

Future<FBApi> signInWithGoogle() async {

static Future<FBApi> signInWithGoogle() async {

signInWithGoogle在不首先创建实例的情况下使其可用。

我不知道真正的意图是什么。


推荐阅读