我还在尝试制作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();
}
}
答案 0 :(得分:0)
在您收回回调结果之前,您过早地调用了result.success
。我认为您应该在result.success(token);
内移动onSuccess
并处理onCancel
中的错误案例。您应该删除AccessToken
私有成员变量。