Home  »  Blog  »  Intro: Dart Basics Part 1

Intro: Dart Basics Part 1

In this blog we are going to talk about the basics of Dart. Dart is a very simple language which has a C like syntax, So if you’re coming from a C++ or C# background or even Javascript it’ll feel just like home for you guys and even if you’re new to programming and everything don’t worry we’ll walk through everything from the base.

Introduction

Dart is an Object Oriented Programming (OOP) language, which means all the data in the project is encapsulated in the forms of objects of a certain class. If you have previous experience with an OOP language that’s good for you & if you have no idea what that means, no need to worry we’ll discuss this in detail in the later parts. Moving on to the next thing, we know that Dart was developed at and by Google, which was primarily used for web development (both front-end & back-end) because Dart can be trans compiled to Javascript.

Features Of Dart

  1. Everything we store in a variable in Dart is an instance of a class, even numbers, functions & even null is an object.
  2. Although Dart is a strongly typed language,but it is also strong in type inference.
    For example: int a = 1;   and var a = 1; both refer to the same meaning that variable ‘a’ store an integer.
  3. Dart supports generic types, which means we can create polymorphic types & that Dart support polymorphism a property of OOP language.
  4. For a Dart program to run there must be a top-level function, a  top-level function is the one which has the priority of being executed first in the program.
  5. Unlike other OOP languages like Java, Kotlin , C++ e.t.c, Dart doesn’t have keyword for the purpose of access specifying like public,private or protected rather it promotes the use of “_” just before the identifier.
    For example: equivalent of private int a = 1; in Dart is int _a = 1;

 

Now since the introductory part is out of the way let’s get to some of the basic concepts before starting. If you are a veteran in programming & for new knowledge please check the next blog, the newbies must focus in these section for a good understanding of what I’ll be taking about in the future blogs.

1.Identifier

An identifier is a name given to a variable, function or a class which are case-sensitive. The some basic rules of naming a identifier are as follows:

  1. They must be unique.
  2.  They are be not longer than 31 characters in most of the programming languages.
  3. The can contain letters, numbers,symbols but they can’t start numbers & special symbols such as . , * ,+ etc.
  4. They can’t be any keywords.

2.Keywords

Keywords are what the name suggests, they are special reserved words that have a specific meaning in any programming language, which means we can’t use them as an identifier.

Some Identifiers used in dart are var , int , ListStream , async , await e.t.c

If you want all the keywords used in dart.Click Here!

3.Variables

Variable are most likely one of the most important part of a program. They are the representation of memory in any program. They store data in a program, which can either be permanent or temporary. The value of a variable can be changed during the execution of a program. The is no default syntax for defining a variable, every programming language has its own way of doing it.

Dart:

 

The First image show variable declaration in Dart with the use of the “var” keyword and the second one shows the use of the int,both of them have the same meaning that we are storing an integer 1 in the variable but in the second example due to the use of the keyword “var” we can store any kind of value to the variable but we can’t do same for the the second case. This is because if we use the keyword “var” to create a variable, it creates a variable with a dynamic type.As the name suggests a dynamic type is the one which takes any value. If you’re thinking now what are these types. Don’t worry we’ll get into them very soon.

 

4.Data Types:

They are the special Keywords that are used to define what type of value does a variable hold. The most common data types are:

  • int: This specifies that the variable stores an integer.
  • String: This specifies that the variable stores a String.
  • bool: This specifies that the variable stores an boolean(either true or false).
  • double: This specifies that the variable stores a floating point value eg: 2.432332 , 1.009 , 1.89 etc.

5.Operators

An operator is a symbol that tells the compiler to perform specific mathematical or logical manipulations. Dart is rich in built-in operators and provide the following types of operators −

  • Arithmetic Operators: They are used to perform arithmetic operations on the operands(data). They consist of the following:-
    Operator Description Example
    + Adds two operands A + B will give 30
    Subtracts second operand from the first A – B will give -10
    * Multiplies both operands A * B will give 200
    / Divides numerator by de-numerator B / A will give 2
    % Modulus Operator and remainder of after an integer division B % A will give 0
    ++ Increment operator, increases integer value by one A++ will give 11
    Decrement operator, decreases integer value by one A– will give 9

 

  • Relational Operators: They are used to evaluate the relation between operands and result in a boolean value. They consist of the following:-
    Operator Description Example
    == Checks if the values of two operands are equal or not, if yes then condition becomes true. (A == B) is not true.
    != Checks if the values of two operands are equal or not, if values are not equal then condition becomes true. (A != B) is true.
    > Checks if the value of left operand is greater than the value of right operand, if yes then condition becomes true. (A > B) is not true.
    < Checks if the value of left operand is less than the value of right operand, if yes then condition becomes true. (A < B) is true.
    >= Checks if the value of left operand is greater than or equal to the value of right operand, if yes then condition becomes true. (A >= B) is not true.
    <= Checks if the value of left operand is less than or equal to the value of right operand, if yes then condition becomes true. (A <= B) is true.

 

  • Logical Operators: They are used to evaluate logical expressions and also return boolean. They consist of the following:-
    Operator Description Example
    && Called Logical AND operator. If both the operands are non-zero, then condition becomes true. (A && B) is false.
    || Called Logical OR Operator. If any of the two operands is non-zero, then condition becomes true. (A || B) is true.
    ! Called Logical NOT Operator. Use to reverses the logical state of its operand. If a condition is true, then Logical NOT operator will make false. !(A && B) is true.
  • Type check Operators: The asis, and is! operators are handy for checking types at runtime.
    Operator Meaning
    as Typecast (also used to specify library prefixes)
    is True if the object has the specified type
    is! False if the object has the specified type

    The result of obj is T is true if obj implements the interface specified by T. For example, obj is Object is always true.

 

  • Assignment Operators: As you’ve already seen, you can assign values using the = operator. To assign only if the assigned-to variable is null, use the ??= operator.

 

  • Bitwise Operator: You can manipulate the individual bits of numbers in Dart. Usually, you’d use these bitwise and shift operators with integers.
    Operator Meaning
    & AND
    | OR
    ^ XOR
    ~expr Unary bitwise complement (0s become 1s; 1s become 0s)
    << Shift left
    >> Shift right

All of this at once may be very overwhelming to you new guys but trust me it’ll come in one piece to you in the future. We’ve now finished the theory part of the basics in the next blog we’ll get into the real deal, So stay tuned.

See ya!!!!.

And if you have any queries leave them in the comment section below.

 

 

The author prefers to keep secret.

In the day to day systems administrator or the increasingly popular DevOps , using the terminal is something that consumes much of our time, whether doing normal tasks, automation…

Start a discussion