Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Flutter communicate with Native code (iOS, Andr...

Avatar for word word
July 27, 2019
130

Flutter communicate with Native code (iOS, Android)

Agenda

- About Flutter
- Flutter Platform Channel
- Demo
- Q & A

Avatar for word

word

July 27, 2019
Tweet

Transcript

  1. *OUSPEVDF അ) Woowahan Bros. / Baemin Market Mobile platform Development

    HydaiCapital / Mobile Platform Development SOCAR Mobile Developer / Mobile , Tablet DreamPlus with Hanwha S&C / ZUMO Application Lassoh inc. (Toronto in Canada) / Mobile SNS platform Samsung Mobile Group / ѓ۟द दܻૉ ѐߊ.
  2. 'MVUUFS4USVDUVSF Native Code Widgets, Rendering Platform Channels Canvas Events Location

    Bluetooth Audio Sensors Camera Etc. Service Platform Application
  3. 'MVUUFS4USVDUVSF Native Code Widgets, Rendering Platform Channels Canvas Events Location

    Bluetooth Audio Sensors Camera Etc. Service Platform Application
  4. 6TF1MVHJOT description: A new Flutter application. dependencies: core: path: ../core

    flutter: sdk: flutter flutter_localizations: sdk: flutter xml: ^3.0.0 flutter_redux: ^0.5.0 intl: ^0.15.2 url_launcher: ^3.0.0 path_provider: ^0.4.0 key_value_store_flutter: ^1.0.0 .... ....
  5. static const channel = const MethodChannel('tryflutter.github.io/flutter_BabyStep'); To define MethodChannel in

    dart we use : Like this… You can name it anything, but the best practice is to name it domain_name/channel_name.
  6. final response = await channel.invokeMethod(message, [optional_arguments]) You can name it

    anything, but the best practice is to name it domain_name/channel_name.
  7. Future<Null> _showDialog() async { final response = await channel.invokeMethod("showDialog", ["Called

    From Flutter"]); final snackbar = new SnackBar(content: new Text(response)); Scaffold.of(context).showSnackBar(snackbar); }
  8. Future<Null> _requestNetwork() async { final response = await channel.invokeMethod( "request",

    ["https://www.thesportsdb.com/api/v1/json/1/"]); Navigator.push( context, new MaterialPageRoute( builder: (context) => new SecondRoute(data: response), ), ); }
  9. channel.setMethodCallHandler { methodCall, result -> val args = methodCall.arguments as

    List<*> val param = args.first() as String when (methodCall.method) { "openPage" -> openSecondActivity(param) "showDialog" -> showDialog(param, result) "request" -> callService(param, result) else -> return@setMethodCallHandler } }
  10. private fun showDialog(content: String, channelResult: MethodChannel.Result) { MaterialDialog.Builder(this).title("Native Dialog").theme(Theme.LIGHT) .content(content)

    .positiveText("Ok") .negativeText("Cancel") .onPositive { _, _ -> channelResult.success("Ok was clicked") } .onNegative { _, _ -> channelResult.success("Cancel was clicked") } .show() }
  11. private fun callService(url: String, channelResult: MethodChannel.Result) { ... service.getEPLTeams().enqueue(object :

    Callback<TeamResponse> { override fun onFailure(call: Call<TeamResponse>?, t: Throwable?) { channelResult.error("FAILURE", "CALL FAILED", t?.localizedMessage) } override fun onResponse(call: Call<TeamResponse>?, response: Response<TeamResponse>?) { channelResult.success(Gson().toJson(response?.body()?.teams)) } }) }