Created
June 26, 2019 17:47
-
-
Save ritshpatidar/11310b6c07653df9d8339ff66524e829 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() | |
} | |
} |
@sonthichaipks I wrote this code almost 3 years ago. You can read this article .
I got an error "Unhandled Exception: PlatformException" in release mode but can work on debug mode.
Could you help me plz ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to write in android code?