Created
June 26, 2019 16:59
-
-
Save ritshpatidar/64e67ea30fd61c2f0311fed94995eb35 to your computer and use it in GitHub Desktop.
Using MethodChannel to connect Kotlin or Java with Flutter.
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
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