×

Dart Basic Program

Dart provides various convenient platforms to code and compile the program in Dart language. Some of them are:

1. Using IDE = Visual Studio code is a text editor with IDE. All you have to do is download the Dart extension, write your code with the .Dart extension and compile using the same.

2. Online Compiler = DartPad is an online compiler provided by the Dart language to code and compile the program in Dart.

3. Using Command line = After downloading Dart SDK, one can easily code in Dart using Dart specific commands in the command line client.

In case you haven’t downloaded it, visit the previous tutorial that discusses the installation of the above platforms in detail.

Let’s write our first program in Dart.

Program:

//This is the first Dart program

void main( )

{

  var a = 5, b = 6 ;

  var c = a + b ;

  print( ' Sum of two numbers is $c. ' ) ;

}
  • Using Visual Studio Code
    • Open Visual Studio Code.
    • Type your code.
    • Save the file with the .Dart extension.
    • Run the code using F5 or the run button on the top right corner.
Dart Basic Program
  • Using online compiler DartPad.
    • Search on Google DartPad.
    • Type your code and run it.
Dart Basic Program
  • Using the command line.
    • Open notepad and type your code.
    • Save the file with the file name as ‘name.Dart’. .Dart to recognize that this is a Dart file.
    • Open the command prompt and write the command ‘Dart filename’. Here, the ‘filename’ is to be replaced with the name of your file.
    • Press enter, and output will be displayed.
Dart Basic Program Dart Basic Program

Let’s understand the program in detail.

This program initializes two numbers, adds them and print their sum.

//This is the first Dart program

void main( )

{

  var a = 5, b = 6;

  var c = a + b;

  print( 'Sum of two numbers is $c.' );

}
  1. This is the first Dart program

The first line beginning with ‘//’ is a single-line comment. Comments do not get executed but are written in order to make the code more descriptive and easier to understand.  

Dart also supports multi-line comments in case of multiple lines as follows:

/*…………………

……………………*/

  • main( )

Like in C, the execution of the program always and only begins from the main( ) function.

  • void

Return type of the function main( ) is void. Return type informs the compiler about the type of value the function will return. void means that the function does not return any value.
It can have other return types as well like, int, double, etc.

  • The whole program is contained within the curly braces { }.  Remember that group of programming statements will always be enclosed within the curly braces {  }.
  • var a = 5, b = 6;

This is the single line, multiple variable initialization statement. ‘var’ is a keyword used to declare a variable without specifying any data type. The Dart compiler automatically interprets the data type of the variable by looking at the value initialized to it. For example, here, the values are integer type.

We can specify the data type explicitly also instead of var keyword. Initialization will look like this in that case,

int a = 5, b = 6;

  • var c = a + b;

This is an assignment statement that performs the arithmetic operation, addition on variables ‘a’ and ‘b’ using plus (addition)  operator ‘+’, and assigns the value of sum on the right-hand side to the variable ‘c’ on the left-hand side using assignment operator ‘=’.

An important point to remember is that in the case of an assignment statement, the value on the right-hand side is always assigned to the variable on the left-hand side. The converse is not true.

  • print(‘Sum of two numbers is $c’);

print is an inbuilt function to display output on the output screen. The text to be printed on the output screen is enclosed within single quotes ‘   ’. ‘$’ operator is used to convert an expression to a string using string interpolation.

  • Remember that every statement is terminated using a terminator ‘;’.

Related Topics

Dart Type inference

The task of interpreting the data types of fields, methods, local variables and most generic type arguments is handled by the static analyser. However, when not enough information is provided...

5 minutes read.

Dart Maps

Map is an object-based data structure that associates keys and values that can be of any data type. Every key can occur only once, while the values can be used multiple...

5 minutes read.

Dart Symbols

Symbols data type in Dart A symbol is an object that is the representation of an operator or identifier in Dart. These are compile-time constants.  They are used in APIs that refer...

1 minute read.

Dart Booleans

Booleans Dart has a data type named ‘bool’ to represent Boolean values : true and false, which are compile-time constants. ‘True’ or ‘False’ cannot be assigned with values 1 or 0. Example, bool...

1 minute read.

Dart Method Overriding

Before understanding method overriding, it is important to clear the concept of polymorphism. Polymorphism is derived from two Greek words 'Poly' which means many and 'morphs' which means many forms....

5 minutes read.

Dart - Control Flow Statements

The control flow statements are used to define the control on the flow of the program. Dart programs are executed in sequential order i.e., the order in which the programming...

1 minute read.

Dart Arithmetic Operators

Arithmetic Operators comprises of all the operators that are used to perform arithmetic operations such as addition, subtraction, multiplication, division, etc. They are binary operators that work upon two operands. Consider A...

2 minutes read.

Dart Single-Page Application Architecture

In a single page application architecture, the source code for a single web page loads the entire application. The responsibility of building the user interface and requesting data from the...

2 minutes read.

Dart Common Collection Methods

Most of the programming languages have arrays as a way to list items together. However, Dart has a collection of data structures similar to array. These are supported by the...

3 minutes read.

Dart Object-Oriented Concepts

Dart is a programming language that supports object-oriented programming. It is focused on the objects that are real-world entities and supports all the concepts of OOPS, such as objects, classes,...

4 minutes read.

Dart Queues

A queue is a user-defined collection of data. It is based on the FIFO principle ( First In First Out ) which implies that the element to be inserted first,...

3 minutes read.

Dart Inheritance

Inheritance is a promising concept of any programming language. It is a property by which a class inherits the property of another class. Moreover, the class deriving the properties of...

5 minutes read.

Unit Testing in Dart

Typing testing ensures that your app keeps working as you add more features or change existing functionality. Unit testing helps to confirm the behavior of a single function, method, or...

4 minutes read.

Dart Standard Input & Output

Standard Input in Dart (stdin): The standard input stream reads data both synchronously and asynchronously from the keyboard.  In Dart programming language, .readLineSync( ) function is used to accept input from the...

3 minutes read.

Dart Comments

Comments are the statements that are used to enhance the readability and understandability of the code. The compiler does not execute these lines. It simply ignores these comments when it scans...

2 minutes read.

Builder Classes in Dart

Before understanding builder classes, let us understand about Flutter that makes the best use of the builder classes. Flutter is actually not a programming language, it is a software development...

6 minutes read.

Dart Construtors

Constructors are the very important concept in any programming language. They are the special functions created in the classes to allocate memory to the objects of that class when created...

4 minutes read.

Dart Function

A Dart function is a collective line of code that are targeted to perform a specialised task. Such lines of code (function) can be reused whenever we need to perform...

6 minutes read.

Dart - extends, with and implements Keywords

The application development in Dart programming language using the Flutter framework, regularly experience different usage of the implements, extends and with keywords. Dart has full support for inheritance that is...

5 minutes read.

Dart Miscellaneous types

Some Other Data types in Dart Apart from the data types studied so far, we have three other data types also which are as follows: NeverDynamicVoid Let us understand each one in detail, Never...

3 minutes read.