Flutter Facebook 登录插件:不注册回调

Posted

技术标签:

【中文标题】Flutter Facebook 登录插件:不注册回调【英文标题】:Flutter Facebook login plugin: Not registering callbacks 【发布时间】:2017-11-28 02:51:20 【问题描述】:

我仍在尝试制作 Flutter Facebook 身份验证插件。我在用户能够登录方面走得更远,但由于某种原因未注册回调。有人有想法吗?

/**
 * FacebookSignInPlugin
 */
public class FacebookSignInPlugin implements MethodCallHandler 

  private FlutterActivity activity;
  CallbackManager callbackManager;
  AccessToken token;

  /**
   * Plugin registration.
   */
  public static void registerWith(Registrar registrar) 
    final MethodChannel channel = new MethodChannel(registrar.messenger(), "facebook_sign_in");
    FacebookSignInPlugin instance = new FacebookSignInPlugin((FlutterActivity) registrar.activity());
    channel.setMethodCallHandler(instance);
  

  private FacebookSignInPlugin(FlutterActivity activity) 
    this.activity = activity;
  


  @Override
  public void onMethodCall(MethodCall call, Result result) 
    if (call.method.equals("signInUser")) 
      FacebookSdk.setApplicationId("XXX");
      FacebookSdk.sdkInitialize(this.activity);
      callbackManager = CallbackManager.Factory.create();
      LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() 
        @Override
        public void onSuccess(LoginResult loginResult) 

          token = loginResult.getAccessToken();
          Log.d("Facebook", token.toString());
        

        @Override
        public void onCancel() 
          System.out.println("cancel");
          Log.d("Facebook", "Cancel");
        

        @Override
        public void onError(FacebookException error) 

        
      );
      login();
      result.success(token);
     else 
      result.notImplemented();
    
  

  public void login() 
    LoginManager.getInstance().logInWithReadPermissions(this.activity, Arrays.asList("public_profile", "user_friends"));
  

  public void logout() 
    LoginManager.getInstance().logOut();
  

【问题讨论】:

【参考方案1】:

在您返回回调结果之前,您调用result.success 太早了。我认为您应该将result.success(token); 移动到onSuccess 中,并处理onCancel 中的错误情况。您应该删除 AccessToken 私有成员变量。

【讨论】:

以上是关于Flutter Facebook 登录插件:不注册回调的主要内容,如果未能解决你的问题,请参考以下文章

注销时flutter_facebook_auth插件未清除数据

Flutter 集成Facebook快捷登录

grails facebook注册/登录

Flutter 插件开发:导入库

Facebook无法在Flutter中使用安装Facebook应用程序登录

Facebook 连接错误