1
votes

Basically I'm invoking a payment sdk in native android(kotlin) using platform channel. SDK is initialised successfully. After payment transaction I will receive the payment status in a kotlin file which actually extends sdk class files (not an activity). From this callback method I need to pass the status to the flutter dart code. Everything runs successfully without any error but while passing the payment status flutter method is not invoked.

Flutter dart code.

@override
void initState() {
  // TODO: implement initState
  super.initState();

  platform.setMethodCallHandler(_handlePaymentResponse); // To call from native android after payment response.

}

// Invoked from Native Android after payment response.
Future<dynamic> _handlePaymentResponse(MethodCall call) async {
  print('_handlePaymentResponse method called');
  switch(call.method) {
    case "message":
      debugPrint('From Native====' + call.arguments);
      return new Future.value("");
  }
}


// On button click this method will be invoked.
Future<void> _initializeSDK() async {

  print('Token $token');

  try {
    await platform.invokeMethod('callPaymentSDK');
  } on PlatformException catch (e) {
    print("Failed to initialize SDK : '${e.message}'.");
  }

}
Native Android : Paymentstatus.kt 

class PaymentResponse() : LibraryPaymentStatusProtocol, Parcelable {

    //val channel = MethodChannel(flutterView, MainActivity.CHANNEL)

    var backgroundFlutterView: FlutterNativeView? = null

    override fun paymentStatus(p0: String?, p1: Activity?) {

        if (p1 != null) {
            Handler(Looper.getMainLooper()).post {
                Log.v("PaymentResponse", "Main thread called")
                backgroundFlutterView = FlutterNativeView(p1, true)
                val channel = MethodChannel(backgroundFlutterView, MainActivity.CHANNEL)
                channel.invokeMethod("message", p0); // Invoking Flutter method 
            }

        }


    }
}
1

1 Answers

1
votes

I ran in to the same issue, and in the end the solution that worked for me (not sure if this is the ideal approach) was to store a reference to the FlutterEngine inside my MainActivity like so:

class MainActivity: FlutterActivity() {
    companion object {
        var flutterEngineInstance: FlutterEngine? = null
    }

    override fun configureFlutterEngine(flutterEngine: FlutterEngine) {
        super.configureFlutterEngine(flutterEngine)
        flutterEngineInstance = flutterEngine
    }
}

In my code I would then just invoke a method like this:

MethodChannel(
    MainActivity.flutterEngineInstance.dartExecutor.binaryMessenger, 
    "com.example"
).invokeMethod("method", mapOf())