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

时间:2017-06-24 18:46:17

标签: java android facebook flutter

我还在尝试制作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 个答案:

答案 0 :(得分:0)

在您收回回调结果之前,您过早地调用了result.success。我认为您应该在result.success(token);内移动onSuccess并处理onCancel中的错误案例。您应该删除AccessToken私有成员变量。