2017-06-24 60 views
0

我仍然試圖製作Flutter Facebook身份驗證插件。我得到了更遠一點的用戶可以登錄,但回調未註冊由於某種原因。有人有想法嗎?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

在回調回調的結果之前,您打電話過早了result.success。我認爲你應該在onSuccess之內移動result.success(token);,並在onCancel中處理錯誤情況。您應該刪除AccessToken私有成員變量。