Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
    }
  }
like image 471
phuocding Avatar asked Jan 29 '19 02:01

phuocding


2 Answers

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

should be

await FBApi().signInWithGoogle();

You first need to create an instance () to call an instance method.

Alternatively you can change

Future<FBApi> signInWithGoogle() async {

to

static Future<FBApi> signInWithGoogle() async {

to make signInWithGoogle available without creating an instance first.

I don't know what the intention actually is.

like image 60
Günter Zöchbauer Avatar answered Nov 01 '22 23:11

Günter Zöchbauer


The analyzer produces this diagnostic when a class name is used to access an instance field. Instance fields don’t exist on a class; they exist only on an instance of the class.

Examples

The following code produces this diagnostic because x is an instance field:

class C {
  static int a;

  int b;
}

int f() => C.b;

Common fixes

If you intend to access a static field, then change the name of the field to an existing static field:

class C {
  static int a;

  int b;
}

int f() => C.a;

If you intend to access the instance field, then use an instance of the class to access the field:

class C {
  static int a;

  int b;
}

int f(C c) => c.b;

Documentation : https://dart.dev/tools/diagnostic-messages#static_access_to_instance_member

like image 35
Paresh Mangukiya Avatar answered Sep 20 '22 14:09

Paresh Mangukiya