Basic Dart Program

Basic Dart Program

This is a simple dart program that prints Hello World on screen. Most programmers write the Hello World program as their first program.

void main() { 
   print("Hello World!"); 
}
Show Output

Basic Dart Program Explained

  • void main() is the starting point where the execution of your program begins. Program starts with main function.
  • The curly braces {} represent the beginning and the ending of a block of code.
  • print(“Hello World!”); prints Hello World! on screen.
  • Each code statement must end with a semicolon.

Basic Dart Program For Printing Name

void main()
{
    var name = "John";
    print(name);
}

Basic Dart Program To Join One Or More Variables

Here $variableName is used to join variables. This $ symbol represents the variable name. This joining process in dart is called string interpolation.

void main(){
  var firstName = "John";
  var lastName = "Doe";
  print("Full name is $firstName $lastName");
}
Show Output

Dart Program For Basic Calculation

Performing addition, subtraction, multiplication, and division in dart.

void main() {
int num1 = 10; //declaring number1
int num2 = 3; //declaring number2
  
// Calculation
int sum = num1 + num2;
int diff = num1 - num2;
int mul = num1 * num2;
double div = num1 / num2; // It is double because it outputs number with decimal.
  
// displaying the output
print("The sum is $sum");
print("The diff is $diff");
print("The mul is $mul");
print("The div is $div");
}
Show Output

Create Full Dart Project

It’s nice to work on a single file, but if your project gets bigger, you need to manage configurations, packages, and assets files. So creating dart project will help you to manage this all.

dart create <project_name>

This will create a simple dart project with some ready-made code.

Steps To Create Dart Project

  • Open folder location on command prompt/terminal.
  • Type dart create project_name (For E.g. dart create first_app)
  • Type cd first_app
  • Type code . to open project with visual studio code
  • To check main dart file go to bin/first_app.dart and edit code.

Run Dart Project

First of all, open project location on the command/terminal and run the project with this command.

dart run