The Dart basics

Dart Learning [Part 1]

The first step to learning Dart…The Dart basics

Shrijit_Basak
My Coding Experiments

--

So now that you are all pumped up about Dart from this previous post, let’s start the learning cycle.

Tutorial Level: ★☆☆☆☆(For Beginners)

Skill level after this post

Dart adapts heavily from Java and if you are already familiar with Java then you are almost a Dart expert. This set of tutorials is heavily referenced from the Google’s codelab which are the actual tutorials published by Google. So please refer to them, if there is any doubt in my tutorials or drop a comment and I will get back to you with proper explanation.

You can use the dartpad for running dart code online.

TLDR;

If you want to read the actual version then skip to this section.

If you are already a bit familiar with the dart jargons or already know Java, Python or JavaScript then you can just have a look at the syntactical structure and move on to the 2nd post.

You can execute the above code here.

Hooray!!…Congratulations!! 🥳🎉.
Now you can move on to the next post and learn the intermediate level stuff about Dart.

Your Dart skill has increased…

The full version…

Every statement in Dart ends with a ; , as we have seen in Java.

The void main (): This is the place where the dart program starts its execution. Anything written inside this block will be executed first and only the code defined elsewhere, referenced in this block will be executed.
Unlike Java, here void main() need not be written inside a class but actually outside any class declaration.

The variables: Dart allows both statically typed and dynamically typed variables. In statically typed variables we provide a ‘datatype’ to mention what kind of values, the variable would hold.
In dynamic type, we only mention the keyword var as the data type and the actual data type is confirmed during runtime.
Like in Python and JavaScript, a string variable can be mentioned either using single quotes ‘’ or double quotes “” or a 3 single quotes ‘‘‘….’’’for multi-line string literal.

The Datatypes: Dart uses both statically typed and dynamically typed data types, i.e. we can either mention the data type during the variable declaration or use the keyword var to declare a dynamic type variable, whose data type will be finalized during runtime.
Dart provides these data types:
1. Number: numerical data using int and double keyword
2. String: using the String keyword
3. Boolean: using the bool keyword. They can hold either true or false value.
4. Function: we can store functions as a variable in dart using the Function keyword and can later invoke the stored function using the variable name. This is very similar to “JavaScript’s function stored in a variable” feature.

In Dart, if we don’t initialize a variable while declaring it, it is assigned the value null. To check whether a variable is null or not we can compare it with Null or null.
eg: print('${v is Null}'); or print(‘${v == null}’);

Dart also allows us to execute code snippets inside a string by wrapping it around the ${ } block.

The Collections: Dart provides 3 types of collections:

  • List: The list is similar to List in Java or in Python. It basically stores a collection of items belonging to the same data type. Each of these items can be accessed using the index at which they are stored. The index starts from 0.
  • Map: The map stores a collection of key, value pairs. It is similar to Map class in Java and ‘dict’ in Python.
  • Set: A set is very similar to List where it stores a collection of items belonging to the similar data types. It is a very useful data structure in certain scenarios like
    a) Checking if an item is present in a list
    b) Checking if 2 lists are equal
    c) Storing a collection when the order of the items in the collection is not important

Variable Security: The variables can be made public or private based on how much we want to expose them. This concept is similar to that of Java. But here we only have 2 levels of security(there are 4 in Java). If we don't specify any keyword while declaring a variable for the security then it is considered as public, but we mention an ‘_’(underscore) as the first alphabet of the variable name (eg: int _n = 5;) then it is considered to be private and it can’t be accessed by any code outside the file in which it is defined. This concept will make more sense when we learn about inheritance.

Hooray!!…Congratulations!! 🥳🎉.

<GTA SanAndreas theme playing in the background!!>

You have now learnt the basics of Dart and your Dart skill has now increased.

Your Skill Meter…

Let’s fill up this meter by continuing this learning in this next post.

I hope you had fun learning the basics of Dart today, this post is just a beginning to the whole series up ahead. Please do give this post a Clap and follow me for more Dart tutorials and other contents.

--

--

Shrijit_Basak
My Coding Experiments

Computer Science enthusiast, gamer, Linux fan(i use arch btw….), foodie.