A tour with Dart syntax - Dart programming - Part 2
Tags: flutter, dart, beginners, mobile
Published: 2022-04-18 07:17:57

Hello. It's **FlutterWithMe** again. After a previous article for introduce overview about **Dart**. This article i'll move on **Syntax in Dart** to help you more understand about dart programming. Let's start with following basic code!

```
void printInteger(int aNumber) {
  print('The number is $aNumber.');
}

void main() {
  var number = 42;
  printInteger(number);
}
```

- **main()** is the **special, required and top-level function** where app execution starts. Every app **must have a top-level main() function**.
- **var** is a way to declare a variable without specifiy its type. But do you know the **difference between var and dynamic** ? Let me quick compare to you guys.

> - dynamic: can change TYPE & VALUE of the variable later in code.
> - var: takes the type of the value that is first assigned and doesn’t allow the type to be changed, only can change value.

> Dart is smart enough to know exact type when you declare var with variable. On previous example type of number is : "int".

- **void printInteger(int aNumber)** is a way to **declare function**. Dart is a **true object-oriented language**, so **functions are object and have a type**. On previous example function type is void so no have return variable on function.

- **print("message")** is a function to **display output to console log**. 

Below are these keyword that Dart language treats specially. Avoid using these keyword as identifiers.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bdmkjdj7aumsfosyg4ab.png)

Now these are important concepts when you learn Dart languages:
- Everything you place in a variable is an object.
- If you enable null-safety, variable can't contain null unless you make it can.You can make a variable nullable by putting a question mark (?) at the end of its type. 

> Example: 
> ```
> int? number;
> ```
> That's mean number variable can be **int type** or **null**
>  
> But if you do like this 
> ```
> int plusNumber = 1 + number!;
> ```
> That's mean you're force number never null with "!" comma. And if number null, it'll through an exception. So avoid doing like that if you not sure about data can null or nut.

- Dart support generic types like List<int> or List<Object> that's depend on type you pass into generic comma.
- Unlike java, Dart doesn't have keyword public, protected, private. If an identifier start with underscore (_), it's private to this class.
```
int number; //This is public variable
int _number2: //This is private variable
```

- Remember to check dart analyze for make a better code. Because it'll follow to dart coding convention rules. Which i'll talk about in other article.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4tfoc2wg6fpdd81892t4.png)

I think it's enough to understand about dart syntax. To sum up about dart syntax :
- Support top-level functions.
- Have null-safety which help us avoid NullPointException like java when get null value.
- Dart define private variable with underscore (_).
- Remember to check dart analyze frequently for better code convention.

So thank you for spend time reading some of my dummy article. I want to take a good preparation before going into some awesome knowledge about Flutter in the future. If you have any advise or comment, please let me know so i can update and sharing my knowledge more easy understanding.