 Hey everybody, it's Brian and I wanted to do a Dart video because I've been doing a lot of cute videos lately. So what we're going to cover is how to deploy a Dart application using the Dart to native. So first off, what is Dart to native? Well, it's part of the newer Dart, and it takes your Dart project and compiles it into a single standalone executable. That's right. Get excited. This is extremely cool. So instead of having to ship your script with a copy of the Dart virtual machine and then doing a Dart and then run your script, you can actually just compile it down to one file and hand them a single file. Amazing. I love this. And it uses ahead-of-time compilation. So this is lightning fast. So let's go ahead and take a look at how to do this. We're going to use IntelliJ. Just going to make a new project. I have as of today the newest version of Dart 261. We're going to do a console application. And let's call this Downloader. And we have our simple little boilerplate that comes as default, which is just going to print out 42 if we run this. It says, share it off, hello, 42. So not a super, super complex application by any shape of the imagination, but it's got a little bit of complexity, right? So we've got our main, and then we've got this lib folder with downloader in it, and that just has our calculate function. And then we're importing that as a package. So let's take this and look at how we would run something. So here's the folder it's in, and we're going to just open in terminal. The traditional way of doing this is you would use the Dart VM. So we would say Dart, and you see there's tons of options. Clear that out. Let's say Dart and let's say bin main Dart. And then ta-da, it's calling that. So unfortunately, doing it this way the old way, you of course have to include the Dart virtual machine, which means your non technical users are going to have to go out here and download and install Dart, or you're going to have to make some sort of installer and do it for them. We don't want to do any of that. So let's explore the start to native. Jump back out here. And we're going to say Dart to native. And here we want to give it a few little options, but let's explore this real quick. So you can see how we have it defined, enable inserts, help. A lot of this is vastly unhelpful for us. Really what we want to do is take this, give it a file with some options. The biggest one we need to pay attention to is dash o for output. This is the file we're going to actually create. So like on a Windows box, it would be something.exe. Anything else. It's just some sort of executable binary. And packages. So if you try to do this and you get a big fact, could not find packages, you need to actually give it the folder to it. First time I ran this, it complained that it could not find the actual, this little guy, this downloader.dart. Couldn't find that. And I had to give it the full path to it. And that was on a different machine. And I think Dart was hosed up. I just reinstalled Dart and it magically started working. So back into here. Let's clear this out. And we're going to say Dart to native. Going to give it our file. And then we want an output file. And we're going to say hello. And I'm going to actually move this down so you can see it be created up here. And it's just going to take a second. And it generated the hello. Now if you're on a Windows box, this will be an exe, everything else. It'll just be an executable. And if we just say, you can see it is about eight megs in size. So it's kind of big. What's going on in there? Well, really, it's taking your script, your little main Dart, going through finding all the dependencies and packages and everything it needs and compiling it down into this intermediate style language. And I'm skipping a lot of steps here and simplifying this and then putting a Dart front in or a Dart loader and just running it. Now that's not entirely accurate. This actually does compile it down into ones and zeros. And then you have just a binary boot. I think it's called a booter, a bootloader in front of it. I probably said that horribly incorrect, but you get the point. So we're just going to run our hello app and you see hello world 42. So let's go ahead and time this bad boy. You can see it's pretty snappy. I mean, it's pretty fast just to run this bad boy. Now I realize this is a trivial example. So let's just blow this away and do something a little bit more complex here. And it's been a while since I've written any Dart code. So bear with me here. We're going to import the HTTP package. And we want to import this as, of course, HTTP. And then let's go ahead and import our darts async. I want to do a little bit of a synchronous programming here. Main. I probably could just copy and paste a lot of this, but we can just type it out super quick. So I'm going to say list. And we want a list of strings. These are our arguments. And let's make this whole thing async. And now what we want to do is we want to actually say, hey, check to make sure that user gave us an argument. If there's no argument, then we want to give them an example of what they should do. So if args length is zero, tell the user, hey, you screwed up. And whenever I do something like this, I like to give my users an example because let's just face it sometimes and users are not technical at all. Let's go ahead and edit our configuration. We've got no arguments. So let's go ahead and test this, save and run. It should pop back and say usage. There we go. Now, if we've got the actual argument in there, then we want to do something else. I'm going to say URL. And we want the element at the zero position because we're going to get the first element. And we're going to say response. And we're going to await the HTTP get and just let that churn away and download for us. And then we want to print out the length here. So we're going to say, wow, it has been a while since I've done any real hardcore typing. All right, let's do this. Hey, let's say response. And we want to get the content length. And we could, if we wanted to do the body as well, get the actual code. I'm going to comment that out for now just to show you, hey, this works. So we're going to take this, I'm going to edit our configuration, we're going to give it an argument starting. And there's the length 50, 60. And just to show you that this is working, we're going to uncomment that rerun it, and it'll show you the actual HTML that it just downloaded. And let's re-comment that because it's kind of ugly. Save, run, just a test. Ta-da, it works. Let's jump back out here. We have no downloader. So what we're going to do now is we're going to just make an entirely new executable called downloader. And it's going to take just a second. And let's go ahead and test that. We forgot our little argument, see? And users, man, I'm telling you. But did we screw up here? Oh, yes. See, I'm telling you. End users. HTTP. We could figure this out. We're smart. Ah, yes, there we go. And ta-da, it's now magically working. So this is a pretty solid demonstration of what's going on here. End users make mistakes. We can still run. Let's actually time this thing. It's not really a fair test because, well, we are doing some network stuff, but you can see right off the bat. And it's working as expected. Now, there are some caveats here. If you have some missing packages or anything like that, when you go to actually do your Dart to native, you're going to have to give it the packages with dash P packages. And if you try to do any sort of executable packing, you may have a bad time. Now, what do I mean by that? So let's go back here. And here's our downloader in all its glory. In case you're curious, this is only eight mags. And I say only, but there's a lot of functionality baked in there. So we could actually compress this into a zip, tar, gz, whatever we want. And this little guy will run just fine. And we could ship this zip to the users. And when we go properties, it's only 2.8. So what an executable packer does is it would do something similar to this where it compresses it down, but it makes this executable. And let's go ahead and look at that super, super quick. All right, let's open up our command line. I'm going to use UPX. If you've never heard of UPX, it is the ultimate packer for executables. And this thing's pretty awesome. I've used this for years. So what we need to do now is UPX, we're going to give it a dash nine because we want the best compression possible. And then we need to give it our file name. In this case, downloader run that. This is going to take just a second. It's going to grind away. And it's going to do a lot of super awesome compression. And you can see, boom, 30% right off the bat. This is only 2.5 mags. And you can see here is our 2.5 mag downloader. And it's pretty comparable to our zip file. Now I have absolutely zero faith this is going to work. Your results may vary. But if I just try to run that packed executable, nope, did not work. We got a whole lot of crap. So it says usage dart. You notice how they're also doing that? Usage dart meaning this is that internal dart VM trying to figure out what we're trying to do because the address, the starting position of our code that we baked in there has now changed because we've changed the binary. So that will not work. But let's just get rid of this little guy and let's extract him. So we're going to extract him here. Here's our downloader. You can see it's the original eight and it will work just fine. So when in doubt, use dart to native and then compress it as a zip if it's just too big for you. I know this day and age eight mags really is not that big at all. But don't go delving into executable packers. You're going to have all sorts of craziness happen and you just want to avoid that. So when in doubt, stay safe. I hope you found this educational and entertaining. I do cover a lot of dart programming out on you to me. Feel free to just jump in and dive into those courses. I actually give those courses away for free quite a bit. I'm a real big fan of dart. But just wanted to drop a note and give back to the community and thank you for watching.