Skip to content

Instantly share code, notes, and snippets.

@taghassan54
Forked from ritshpatidar/methodchannel.kt
Created January 8, 2022 19:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taghassan54/c8a80183fe3d5d6dc4ccb2e8156d1317 to your computer and use it in GitHub Desktop.
Save taghassan54/c8a80183fe3d5d6dc4ccb2e8156d1317 to your computer and use it in GitHub Desktop.
Connecting Java or Kotlin to Flutter using MethodChannel
package com.example.ourproject
import android.os.Bundle
import io.flutter.plugin.common.MethodChannel
import android.widget.Toast
import io.flutter.app.FlutterActivity
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() {
private val CHANNEL = "ourproject.sendstring"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
GeneratedPluginRegistrant.registerWith(this)
MethodChannel(flutterView, CHANNEL).setMethodCallHandler { call, result ->
if (call.method == "callSendStringFun") {
showHelloFromFlutter(call.argument("arg"))
val temp = sendString()
result.success(temp)
} else {
result.notImplemented()
}
}
}
private fun sendString(): String {
val stringToSend: String = "Hello from Kotlin"
return stringToSend
}
private fun showHelloFromFlutter(argFromFlutter : String?){
Toast.makeText(this, argFromFlutter, Toast.LENGTH_SHORT).show()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment