Call Android Kotlin Code

8 minutes
Share the link to this page
You need to have access to the item to view this lesson.
One-time Fee
List Price:  $139.99
You save:  $40
List Price:  €128.79
You save:  €36.80
List Price:  £110.19
You save:  £31.48
List Price:  CA$190.57
You save:  CA$54.45
List Price:  A$209.11
You save:  A$59.75
List Price:  S$188.40
You save:  S$53.83
List Price:  HK$1,091.62
You save:  HK$311.91
CHF 90.88
List Price:  CHF 127.24
You save:  CHF 36.35
NOK kr1,067.14
List Price:  NOK kr1,494.05
You save:  NOK kr426.90
DKK kr686.38
List Price:  DKK kr960.96
You save:  DKK kr274.58
List Price:  NZ$228.66
You save:  NZ$65.33
List Price:  د.إ514.18
You save:  د.إ146.92
List Price:  ৳16,338.55
You save:  ৳4,668.49
List Price:  ₹11,658
You save:  ₹3,331.09
List Price:  RM655.85
You save:  RM187.40
List Price:  ₦210,474.96
You save:  ₦60,140
List Price:  ₨38,865.72
You save:  ₨11,105.28
List Price:  ฿5,043.69
You save:  ฿1,441.16
List Price:  ₺4,508.30
You save:  ₺1,288.17
List Price:  B$714.43
You save:  B$204.14
List Price:  R2,546.38
You save:  R727.58
List Price:  Лв251.52
You save:  Лв71.86
List Price:  ₩190,204.11
You save:  ₩54,347.91
List Price:  ₪517.99
You save:  ₪148
List Price:  ₱8,104.60
You save:  ₱2,315.76
List Price:  ¥21,798.54
You save:  ¥6,228.60
List Price:  MX$2,323.18
You save:  MX$663.81
List Price:  QR508.83
You save:  QR145.39
List Price:  P1,890.63
You save:  P540.21
List Price:  KSh18,338.69
You save:  KSh5,240
List Price:  E£6,552.93
You save:  E£1,872.39
List Price:  ብር8,014.55
You save:  ብር2,290.03
List Price:  Kz118,777.64
You save:  Kz33,938.89
List Price:  CLP$124,991.07
You save:  CLP$35,714.28
List Price:  CN¥993.35
You save:  CN¥283.83
List Price:  RD$8,173.51
You save:  RD$2,335.45
List Price:  DA18,828.31
You save:  DA5,379.90
List Price:  FJ$311.92
You save:  FJ$89.12
List Price:  Q1,084
You save:  Q309.73
List Price:  GY$29,190.54
You save:  GY$8,340.75
ISK kr13,827.61
List Price:  ISK kr19,359.21
You save:  ISK kr5,531.60
List Price:  DH1,389.16
You save:  DH396.93
List Price:  L2,472.24
You save:  L706.40
List Price:  ден7,927.38
You save:  ден2,265.13
List Price:  MOP$1,121.33
You save:  MOP$320.40
List Price:  N$2,552.76
You save:  N$729.41
List Price:  C$5,136.75
You save:  C$1,467.74
List Price:  रु18,602.11
You save:  रु5,315.27
List Price:  S/521.05
You save:  S/148.88
List Price:  K541.86
You save:  K154.83
List Price:  SAR525.01
You save:  SAR150.01
List Price:  ZK3,561.44
You save:  ZK1,017.62
List Price:  L640.73
You save:  L183.08
List Price:  Kč3,182.25
You save:  Kč909.28
List Price:  Ft49,718.89
You save:  Ft14,206.41
SEK kr1,068.43
List Price:  SEK kr1,495.85
You save:  SEK kr427.41
List Price:  ARS$124,135.76
You save:  ARS$35,469.89
List Price:  Bs964.05
You save:  Bs275.46
List Price:  COP$534,257.88
You save:  COP$152,656.01
List Price:  ₡71,411.70
You save:  ₡20,404.80
List Price:  L3,454.78
You save:  L987.15
List Price:  ₲1,045,465.66
You save:  ₲298,725.81
List Price:  $U5,431.06
You save:  $U1,551.84
List Price:  zł548.49
You save:  zł156.72
Already have an account? Log In


Hello, in this video, I'm going to show you how to connect up your flutter Dart based project to native kotlin code. If you're interested in doing it for Java, I've got a couple of videos covering that. So feel free to check them out. This is for kotlin. So I'm just going to close this down a sec. So for kotlin, you do need to create a project in a certain way.

So I'll show you that I've already got created that way. But I'm gonna show you how to do it. So you will make sure you do new flutter project and my web is always coming to my words, my next or you're on Mac or you're on Windows, you want to go to flutter application. And now what do you want, I'm not going to actually create this I want to show you what you need to select to go to next. You need to select this include kotlin support for Android code. If you do not do that.

It's very difficult to get kotlin code in there. You know Working hundred percent. So that's why I would recommend men especially if you know that you are going to be you know, doing some kotlin. Okay, and then click finish, you create your project and then you're basically in a similar position to where I'm in. So what you want to first do is import package, colon flutter for slash services dot Dart. Hold on there.

Now the error disappeared fantastic. Scroll down to this. underscore my home page state in here. We want to do static content, Khan's platform. So this is gonna be the connection to our kotlin code. This is gonna be equal to on message channel.

And here, you can put wherever you want. It's good to put it into a for a package name formatting, do calm dot turb dot epic for slash f No, you could just have you like this or like, that's totally up to you. I like to do it like this and then like this. So I can sort of say, this is, you know, the single channel that I'm connecting to my kotlin. But I could add sub channels who could have multiple channels in the kotlin code, the person Hold on there. Now what we want to do is we are going to create a method and it's going to be a void, empty, and this simply is going to print out a result that we get back from our kotlin code when we call a particular method, and we'll do a thing and screen value.

So we are going to do it slowly block. In here we're going to say value equals a weight platform dot invoke method. And we are going to put 20 lists and this does not have to match this has to match in the kotlin side, which are showing you the second, this is going to be a catch block. For this wisdom, print out the error, if we actually get any we shouldn't do it. But just in case and we are going to print out this value as well, you don't have to print it out, you could assign it to some variable that maybe you've created here. And you can use that on some other button press, for example.

So now that's actually going to close that down, working or done here. What you want to do is go to your Android folder, app, SRC main kotlin comm dot example whatever the package name is, and main activity, ignore this red line. This is what your file would look like, by default. It's just unable to get the Android import. And if you was editing in Android Studio, which you can do so in another window, but you know, for our purposes, it's not necessary. So in here, we can actually know, first of all, thou shalt know.

Equals, and this has to be the same as what we set our method channel here that platform so just copy it to make sure you don't get any errors or any you know mistake. Now inside of the on create, just afterwards, you want to say and you know semicolons aren't required in kotlin so we can get rid of that. He's gonna put method channel inside of here, not terview Hama channel and the do dot set method call handler inside of here Look missing something Nope, we don't want bracket one curly braces. And inside of here, we are going to say method for results. So essentially like so. And this will get triggered when ever some method is trying to invoke this particular platform.

So this right here, because we put out multiple methods and multiple methods, you just change this to a different method name, we want to handle what method is actually being triggered. And we can simply do that by some method call dot method is equal to printing. So remember, there has to match exactly what we put here. If you put brackets here, make sure you put them over there. You do not need them even though it is a method. And now we're done.

We've actually handled it so this Right here, this code inside of this system will get triggered when and only when this particular platform invokes a method called printed. So in in here, if you want to return a value back into it, you could do some calculations, whatever you want with your return something back, do we build dot success, and then you put the object here, I'm just going to put a string. And I'll say, Hello, from the lean world. Yeah, that's it. We're all ready to go. So one thing to bear in mind in make sure you shut down the application, if you already have it running because hot reload and hot restart will not work on building the Android, you know, project side of, you know, what we are doing.

And you will need to do a proper restart or a proper you know, debug, stop. To be able to build the unrestored side. And that will be reflected in the new APK that is installed for you just do a modification in the dot and not in the kotlin. then most of the time hot reload and hot restart works. So we've got this button. So if I click it that's because the unpress I haven't triggered this method printing.

There we go. So now we should just be able to do a hot reload because this will just on the dark side of things. So if I click the method, it says hello from the kotlin world. And as you can see, none of that Hello from the kotlin world is in the dark side. It's in the kotlin solid. So that's how you call a method from your flutter dot sight to your Android kotlin file.

If you have any questions, feel free to shoot me a message. In the next video I'm going to show you how to pass parameters in to the kotlin file plus will be really cool and I look forward to seeing you very soon.

Sign Up


Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.