Dart for beginners - Part 1

Dart for beginners - Part 1

What is Dart

Dart is a programming language designed for client development, such as for the web and mobile apps. It is developed by Google and can also be used to build server and desktop applications. Dart is an object-oriented, class-based, garbage-collected language with C-style syntax.

The above is the definition from Wiki. Dart has recently garnered a lot of attention after flutter gained popularity for app development. Hence we need to get familiar with Dart before we can start app development in flutter.

Let's get started !

Hello World !

If you are familiar with the programming world, the first program we write is printing the string "Hello world" so here's how simple it looks:

main() {
  // Print 'Hello World'
  print("Hello World");

Let us try to breakdown the above program and see the various components of the program:

  • main(): Similar to C, main() is the entry point of a dart program
  • similar to C, statements end in semi-colons(;)
  • curly braces {} are used to denote a code block unlike Python which only depends on indentation.
  • comments start with //

Taking User Input

import 'dart:io';

main() {
    print("Hello " + stdin.readLineSync());

Here we see few more components:

  • import lets us access existing libraries in our code
  • dart:io library here provides I/O (Input/Output) support
  • stdin.readLineSync() is used to take user input
  • + inside print() method is used to concatenate the two strings.

Everything is an object !

As mentioned before, Dart is an object oriented language where everything is an object.

Everything you can place in a variable is an object, and every object is an instance of a class. Even numbers, functions, and null are objects. With the exception of null (if you enable sound null safety), all objects inherit from the Object class. - Dart language tour

Data types and variables

Variables are name or reference used to store and manipulate data. This is how the structure of variable declaration looks in Dart:

image.png Here's an example:

main() {
  // initialize and print variable
  int myVar = 5;

Data types of a variable is an attribute which tells us what type of value that variable is storing. These are the in-built data types supported by Dart

  • Numbers
  • Strings
  • Booleans
  • Lists
  • Sets
  • Maps
  • Runes
  • Symbols

In this article, we are going to explore basics of numbers, strings, and booleans. We'll cover the rest in later articles of this Dart series.

Points to note

  • All data types are objects
  • Uninitialized variables of any datatype will have an initial value of null

The num Type

We use num type if we want to store any numbers. It can store both integers and floating point numbers.

main() {
  num num1 = 15;
  num num2= 25.1;

  // Print values

Dart num has two sub-types:

  • int for integers
  • double for floating point numbers

The int type

Integers are whole numbers without any decimal point. In this example we can see that an int type variable can have both hex or decimal number.

main() {
  int decimal = 1;
  int hex = 0x12AB;

  // print the numbers

# output

The double type

The numbers with decimal point are represented by double type.

main() {
  double num = 1.25;
  double exp = 1.25e5;

  // Driver Code
# output

The string type

A set of characters is called a string. eg "abcde", "this is Dart" etc

main() {
  // Single Quotes
  print('Using single" quotes');

  // Single quotes with escape character \
  print('It\'s fun to code in Dart');

  // Double quotes
  print("It's fun to code in Dart");

  // Initialize a string variable
  String s1 = "A String";

As we can see above, we can use both single and double quotes to create a string. We do not need to escape single quotes inside double quotes and vice versa.

Now let's see how to use a variable or expressions when printing a string

main() {
  String lang = "Flutter";
  print("I want to learn $lang"); // output: I want to learn Flutter
  print("5 + 7 = ${5+7}.");

The bool type

The bool type variable stores only true or false values.

main() {
  bool b1 = true;
  print(b1);           // prints true
  print (5 > 9);     // prints false

That's it for now

Now that we have familiarized ourselves with the Dart program structure and basic/primitive datatypes, let's wrap up our discussion. You can practice your Dart skills on Dartpad which is an online Dart editor.

Next part of this tutorial will cover the various operators that we have in Dart so follow my blog to stay updated.

You can also find me on Twitter

Happy Coding. Cheers 🍻

Share this