Explore the basics of Dart, a programming language that allows you to write more complex logic code.
Variables and data types
Dart is a language that focuses on statically typed objects. Variables that are not assigned a value at initialization have a null value by default. You can also assign a null value to a variable when you declare it, or at any point in your programming.
Basic data types
int, double, bool, String is a basic data type in Dart. During the lifetime of the variable, it is used only with the first defined data type. Attempting to assign different data types will result in a compilation error.
The infinity value is a static property of the double class and can be accessed using double.infinity
Var keyword
You can assign any value to a variable declared with type var and Dart will automatically infer the data from the initialization value. Type-inference is a built-in feature of Dart.
Num data type
The num keyword is also in dart, allowing you to create variables with both int and double. A use case is when a variable can hold a value of type int or double at any time.
String
Strings can be declared using single quotes ” or double quotes ” “, similar to Javascript. Single quotes ”’ or double quotes ” can also split a line into multiple lines of code.
Dynamic data types
You can explicitly declare a dynamic variable using the dynamic keyword. Or it can be automatically assigned by dart when the data type is not explicitly declared.
Note
Always use dynamic data types with caution and explicitly declare data types as often as possible. Otherwise you may introduce bugs in your code. Caution: Only use dynamic if you know very well what you are doing.
Function
Dart is a typed language. So anything in Dart (including functions) has a type. See the main function example below.
Main function
The return type of this function is void. Dart automatically calls main(){…} when your app starts. This is a simple for loop that executes when this main function is called.
In general, you can name any function you want, but main is a special name because it’s the launching point of your Dart app.
Note
You must follow the camel-case convention for naming functions. i.e.yourFunctionName(){}
How to use functions
As explained above, functions can be defined and named in camel-case. If you return a value from a function, you must declare the return type of the function. It’s not mandatory, but if you don’t do it, Dart will run it with a warning instead of an error. Let’s look at this example of a function with and without a return type.
Now let’s consider the first function addTwoNumbers(…){…}. This function has no explicitly declared return and argument types. So Dart parses the grammar and implements this function as follows:
Dart automatically assigns dynamic types to functions and arguments. You can pass any data type as an argument to this function and dart will try to add it. However, this way of declaring functions is error-prone. The reason is that when you can add int and bool types, you get a runtime error instead of a compile error!
The second function void addTwoIntegerNumbers(int num1, int num2){…} is less error prone because it explicitly declares void and int return and argument types. As soon as you pass non-int data to this function, the compiler will warn you.
3rd function int sumOfTwoNumbers(int num1, int num2){…} but return from function so return type is int instead of sum.
In the main function, add parentheses () to call and implement a method with this name.
Attention again! Avoid dynamic types!
Dart is a mix between strongly-typed and loosely-typed languages, but you should avoid dynamic types as much as possible, as shown in the example above. This makes the code more stable and error free.