-
-
Save taghassan54/c8a80183fe3d5d6dc4ccb2e8156d1317 to your computer and use it in GitHub Desktop.
Connecting Java or Kotlin to Flutter using MethodChannel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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