diff --git a/android/src/main/kotlin/za/co/britehouse/flutter_microsoft_authentication/FlutterMicrosoftAuthenticationPlugin.kt b/android/src/main/kotlin/za/co/britehouse/flutter_microsoft_authentication/FlutterMicrosoftAuthenticationPlugin.kt index 580de24..a7d5811 100644 --- a/android/src/main/kotlin/za/co/britehouse/flutter_microsoft_authentication/FlutterMicrosoftAuthenticationPlugin.kt +++ b/android/src/main/kotlin/za/co/britehouse/flutter_microsoft_authentication/FlutterMicrosoftAuthenticationPlugin.kt @@ -67,7 +67,7 @@ class FlutterMicrosoftAuthenticationPlugin: MethodCallHandler { "acquireTokenSilently" -> acquireTokenSilently(scopes, authority, result) "loadAccount" -> loadAccount(result) "signOut" -> signOut(result) - "init" -> initPlugin(configPath) + "init" -> initPlugin(configPath, result) else -> result.notImplemented() } @@ -103,11 +103,11 @@ class FlutterMicrosoftAuthenticationPlugin: MethodCallHandler { } } - private fun initPlugin(assetPath: String) { - createSingleAccountPublicClientApplication(assetPath) + private fun initPlugin(assetPath: String, result: Result) { + createSingleAccountPublicClientApplication(assetPath, result) } - private fun createSingleAccountPublicClientApplication(assetPath: String) { + private fun createSingleAccountPublicClientApplication(assetPath: String, result: Result) { val configFile = getConfigFile(assetPath) val context: Context = mainActivity.applicationContext @@ -123,10 +123,12 @@ class FlutterMicrosoftAuthenticationPlugin: MethodCallHandler { */ Log.d(TAG, "INITIALIZED") mSingleAccountApp = application + result.success(null) } override fun onError(exception: MsalException) { Log.e(TAG, exception.message) + result.error(exception.errorCode, exception.message, null) } }) } diff --git a/lib/flutter_microsoft_authentication.dart b/lib/flutter_microsoft_authentication.dart index a1d4359..da72676 100644 --- a/lib/flutter_microsoft_authentication.dart +++ b/lib/flutter_microsoft_authentication.dart @@ -9,6 +9,7 @@ class FlutterMicrosoftAuthentication { List _kScopes; String _kClientID, _kAuthority; String _androidConfigAssetPath; + Future initialFuture; FlutterMicrosoftAuthentication( {String kClientID, @@ -20,8 +21,22 @@ class FlutterMicrosoftAuthentication { _kScopes = kScopes; _androidConfigAssetPath = androidConfigAssetPath; - if (Platform.isAndroid) - _channel.invokeMethod("init", _createMethodcallArguments()); + if (Platform.isAndroid) { + initialFuture = _channel.invokeMethod("init", _createMethodcallArguments()); + } + } + + /// Make sure the plugin has been ready. + /// + /// To prevent potential problem, + /// you should call this after instantiate [FlutterMicrosoftAuthentication]. + /// + /// ``dart + /// final fma = FlutterMicrosoftAuthentication(); + /// await fma.ensureInitialized(); + /// ``` + Future ensureInitialized () { + return Future.value(initialFuture); } Map _createMethodcallArguments() {