Skip to content

Instantly share code, notes, and snippets.

@ritshpatidar
Created June 26, 2019 16:59
Show Gist options
  • Save ritshpatidar/64e67ea30fd61c2f0311fed94995eb35 to your computer and use it in GitHub Desktop.
Save ritshpatidar/64e67ea30fd61c2f0311fed94995eb35 to your computer and use it in GitHub Desktop.
Using MethodChannel to connect Kotlin or Java with Flutter.
class _MyHomePageState extends State<MyHomePage> {
static const platform = const MethodChannel('ourproject.sendstring');
String receivedString = "";
Future<void> callNativeFunction() async {
String msg = "Hello from Flutter",data="";
try {
final String temp = await platform.invokeMethod('callSendStringFun',{"arg":msg});
data = temp;
} on PlatformException catch (e) {
data = "Failed";
}
setState(() {
receivedString = data;
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("CallNative"),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
RaisedButton(
onPressed: callNativeFunction, //callNativeFunction is called on button pressed
child: Text("Call Native"),
),
Text('$receivedString'),
],
),
),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment