DEV Community

Cover image for A tour with Dart syntax - Dart programming - Part 2
Hoang Nguyen
Hoang Nguyen

Posted on

A tour with Dart syntax - Dart programming - Part 2

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);
}
Enter fullscreen mode Exit fullscreen mode
  • 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

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 or List 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
Enter fullscreen mode Exit fullscreen mode
  • 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

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.

Top comments (0)