diff --git a/packages/share_plus/share_plus/android/build.gradle b/packages/share_plus/share_plus/android/build.gradle index d8b6e6c022..bcc5686bc8 100644 --- a/packages/share_plus/share_plus/android/build.gradle +++ b/packages/share_plus/share_plus/android/build.gradle @@ -50,5 +50,6 @@ android { implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version" implementation 'androidx.core:core-ktx:1.16.0' implementation 'androidx.annotation:annotation:1.9.1' + implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.10.1' } } diff --git a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt index 9b3341c8be..abd6efc985 100644 --- a/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt +++ b/packages/share_plus/share_plus/android/src/main/kotlin/dev/fluttercommunity/plus/share/MethodCallHandler.kt @@ -3,6 +3,11 @@ package dev.fluttercommunity.plus.share import android.os.Build import io.flutter.plugin.common.MethodCall import io.flutter.plugin.common.MethodChannel +import kotlinx.coroutines.CoroutineScope +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.SupervisorJob +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext /** Handles the method calls for the plugin. */ internal class MethodCallHandler( @@ -10,6 +15,8 @@ internal class MethodCallHandler( private val manager: ShareSuccessManager, ) : MethodChannel.MethodCallHandler { + private val mainScope = CoroutineScope(Dispatchers.Main + SupervisorJob()) + override fun onMethodCall(call: MethodCall, result: MethodChannel.Result) { expectMapArguments(call) @@ -20,20 +27,26 @@ internal class MethodCallHandler( if (isWithResult) manager.setCallback(result) - try { - when (call.method) { - "share" -> { - share.share( - arguments = call.arguments>()!!, - withResult = isWithResult, - ) - success(isWithResult, result) + when (call.method) { + "share" -> { + mainScope.launch { + try { + val arguments = call.arguments>()!! + withContext(Dispatchers.IO) { + share.share( + arguments = arguments, + withResult = isWithResult, + ) + } + success(isWithResult, result) + } catch (e: Throwable) { + manager.clear() + result.error("Share failed", e.message, e) + } } - else -> result.notImplemented() } - } catch (e: Throwable) { - manager.clear() - result.error("Share failed", e.message, e) + + else -> result.notImplemented() } }