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);
}
}
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With