Flutter:无法使用静态访问权限访问实例成员'signInWithGoogle'. (位于的"static_access_to_instance_member") [英] Flutter: Instance member 'signInWithGoogle' can't be accessed using static access. (static_access_to_instance_member at )

查看:707
本文介绍了Flutter:无法使用静态访问权限访问实例成员'signInWithGoogle'. (位于的"static_access_to_instance_member")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试为Flutter项目做绒毛,我有一个用于登录和注销Google帐户的类API,Linter希望在使用这些方法(使用Google登录并注销)之前先删除static.我无法在视图中调用这些函数.这是我的代码:

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错误

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

Logout.dart

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

推荐答案

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

应该是

await FBApi().signInWithGoogle();

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

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

或者,您可以更改

Future<FBApi> signInWithGoogle() async {

static Future<FBApi> signInWithGoogle() async {

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

to make signInWithGoogle available without creating an instance first.

我不知道意图是什么.

这篇关于Flutter:无法使用静态访问权限访问实例成员'signInWithGoogle'. (位于的"static_access_to_instance_member")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆