Get immediate access to this and 4,000+ other videos and books.
Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and
4,000+ professional videos in a single subscription, it's simply the best investment you can make in
your development career.
The Dart programming language was written in 2011 by Google as an alternative way to write JavaScript. JavaScript was just too entrenched and thus Dart didn't catch on but it was later repurposed to write native applications. This evolution made Dart into a just in time compiled language as well as an ahead of time compiled language. So what does that even mean? Well, when we write code, we write it in a specialized language that is designed to be compact in human readable. The computer doesn't understand it. So we have a tool known as a compiler. The compiler will read our code and then translate it into instructions that the computer can understand and actually execute. Compiling can be a slow resource intensive process so oftentimes developers will compile the language into machine instructions and then hand off that binary. When you download an app from the app store, that code has been compiled for you. So all you have to do is simply run it. This is known as ahead of time compiling. Now, when you compile your program you are compiling it for a specific CPU and platform. There's actually another way to compile a program, and that's compiling the code as the program is running, otherwise known as just in time compiling. This makes for the code to be platform independent since the target platform is doing the compiling itself. You may write your code to work on windows, but by utilizing just in time compiling, your program can now run on Mac or on Linux. Most languages are either ahead of time compiled or just in time compiled. Dart is special in that it is both. You can even write code in Dart that can be cross compiled into JavaScript which can then be run in a browser. Conversely, you can compile your code into a binary. This is what allows us to write mobile apps in Dart. So why is this important? Because we can write Dart in a tool called DartPad, this allows us to actually write Dart code in a browser and see the results in real time. This is also what allows us to write flutter code and instantly see the results. Dart is an incredibly flexible language, so let's put it to work. Open up a browser and head on over to DartPad.dev. You'll see a very simple editor with some Dart code already written in it. The left pane allows us to write code, the right pane gives us both our console and documentation. Press the run button. It'll take a moment, but you'll see the results printed out in the console. You can also read documentation about certain commands. Click the print command. You'll see the documentation appear in the documentation pane, along with a link to the official documentation. DartPad isn't just for text, click the samples drop down and select the implicit animations option. Now follow the instructions. Click a disc and you'll see them animate. The code is on the left hand side. It should look a little familiar, that's because it is fluter code. This makes for an excellent tool to test and play around with a fluter. Now click the new pad option, choose dart as the language, and click create. Okay, we're back where we started. First delete everything in the main function so it looks empty. Don't worry about what a function or method is, that's why you're learning Dart. You'll learn about functions soon enough, just know that all your Dart code goes between the two braces. Add the following. Here we defined a variable called welcome message with some simple text. Remember all our statements must end in a semicolon. We also get a squiggle. This is a warning letting us know that the variable isn't being used. Now let's print it out. Now click the run button. And voila, we get a hello world message. Now Dart uses type inference to make our code easier to read. In the case of the welcome message, it's pretty clear we are working with a string by the single quotes so we can just replace the string type with a VAR. VAR lets us know that the text can change. Let's add some more text. We will use the plus equals operator to add some additional text. Here, we just appended some more text using the plus sign. Now click the run button. This works because we change our variable, but let's make another change. This time we get an error because we set the variable to be unchanging, that is a constant. Later we try to change it which causes the error. Let's correct this error by turning it back into a mutable variable. Our variables can also contain numbers such as the following. Here we define the boiling point to be a hundred degrees. This is a whole number. Notice we didn't use the single quotes. If we did, the number would be treated like text. That is a string. We can also define decimal numbers otherwise known as doubles. Here we define a grade point average. Notice it is a type double. Again, the decimal point in 3.5 indicates that it is a double, so we can change it back to a VAR. Now, when you write Dart code, it's really common to want to document what you're doing for yourself or someone you're working with. Or maybe you want to add a note to remind yourself to do something, or maybe you just want to put some dividers in there with some text to help organize your code. But these things aren't code, so how can you tell the Dart compiler to ignore these things? Basically you want to add text that the Dart compiler will ignore. A comment is exactly that. Text that is ignored by the compiler. To add a comment, it's as simple as typing two forward slashes followed by a message. Now click the run button. Our comment is ignored. Mind you this only works on one line. If you add a line break in the middle of your comment, you'll get an error. For cases when you want to write lots of lines, you can write a multiline comment. Start by typing a forward slash followed by an asterisks. All subsequent lines will be ignored until you put a closing comment. Using comments, you can comment out code that you don't want to run. For example, you may not want that print statement to run. Simply put it in a comment and now it won't run. If you comment out code you should only do so temporarily. Remember to delete any unnecessary or unused code from your projects. Commented out code that's left hanging around in your project is only going to confuse you in the future, as well as anyone else working on projects with you.
All videos. All books.
One low price.
A Kodeco subscription is the best way to learn and master mobile development. Learn iOS, Swift, Android, Kotlin, Flutter and Dart development and unlock our massive catalog of 50+ books and 4,000+ videos.