Just get the feel:
1. Why Dart Language?
You can answer this question in one sentence.
Dart is a great fit for both – Mobile Apps and Web Apps. Dart is free and open source and the repository is available at https://github.com/dart-lang. And at the same time you may get the feel of the language at the official website: https://www.dartlang.org/.
In this introductory chapter, let us try to understand why learning Dart language is important for us to build a mission critical Mobile Apps on iOS and Android.
It is important to note, in the very beginning, the developers around the world use Dart to create high-quality apps for iOS and Android and the web. It is feature rich so that client-side development is also possible. As we progress step-by-step, we will see how correct this statement is.
In the first half of the book we will learn Dart language and the second half continues to another book “Beginning Flutter with Dart”, where we will learn Dart alongwith Flutter, a rich and powerful framework for building mobile apps. In that book we will build our first mobile apps from a single codebase on iOS and Android.
If you want to learn building native iOS and Android mobile apps and web apps using Dart, then this book serves as a good introduction because it is designed to give you a complete idea about how Dart works.
Let us have a quick look at the core features of Dart programming language first.
First of all, it is extremely productive. If you already know any object oriented programming language such as C++, C# or Java, it will not take a few days for you to learn Dart language. If you are absolute beginner, then it is good that you are starting to learn your first programming language which has clear and concise syntax. You will also have a great support of rich and powerful core libraries and thousands of packages. As an absolute beginner, you don’t have to worry about the libraries now. We will learn together to use them later when time comes.
Dart is fast and furious, and the performance is high across mobile devices and the web because it optimizes the compilation power. Besides, its portability rate is extremely good. It compiles to ARM and x86 code, so that Dart mobile apps can run on iOS and Android and beyond. Now here is a complementary note for the absolute beginners: there is a difference between ARM and X86 processors; the ARM processors follow a RISC (Reduced Instruction Set Computer) architecture, while x86 processors are CISC (Complex Instruction set Architecture); because of these features x86 processors are considered to be faster than ARM. At the same time for web apps, Dart has a very close
Let us see our first Dart code.
Let us write some more console based code to get the feel of Dart. At the same time we will know what are the most basic syntaxes and how they work together.
//calling a function
//define a function
//calling a function inside another function
//defining another function
print(“Life is too short to do so many things.”);
In the above code, we have defined two functions and nesting another function inside one function we have called them together through a single function.
However, there is a mistake in our code. It is an intended mistake so that you understand how debugging takes place in Dart.
Watch the output:
//output of code 1.2
bin/main.dart:12:24: Error: Expected ‘;’ after this.
We have forgotten to place a semicolon after displaying an output. Let us correct it and run the program again. In Android Studio you may use “Shift+F10” to run the code.
//output of code 1.2
Life is too short to do so many things.
Now it is OK. We have learned many things in our first code. The very first lesson teaches us the most basic thing of all knowledge. You learn from your mistake.
You should always be careful about the syntaxes. Missing a semicolon, or a dollar sign before a variable could be a big game changer. Especially when you are going to build a large scale mobile application on iOS or Android.
You have seen how we have commented on our code. Try to contribute as much comment as possible to clear your standpoint so that when another person reads your code, he/she will understand it, visualize it as you have visualized your code while writing. We will discuss more about comments at the right time.
We have started our code with the top level function “main()”; it is required and special in nature because here the application executes. So inside the main() function we have called a function “doSomething()” which has a nested-function inside it “lifeIsShort()”.
Each function gives a display output with “print()”; it is a handy way to display any output. We have covered many things in our first program.
If you are a complete beginner, you are at a loss to understand the above remarks. You may feel puzzled about those words, such as, function, comment, displaying output etc. Because of that, the next few chapters are dedicated to the beginners. The experienced programmers may skip that pat.