How to declare Variables in Dart

In this article you will learn how to declare variables in Dart Language.
  • 2393

Variable in Dart

A Dart variable referring to memory location used in a computer program or variable are storage location in memory.

Syntax of dart language variable

declared identifier  variable name = value;  i,e; var  name='Dart';

Example of Dart variable

void main() {
  var name='Google Dart !';
  print("Hello,${name}!");

}

Output

var.png

  • Default value

            If you do not assign any value in dart variable; then it returns null.

Example of default value in dart language

void main() {
  var name;
  print("Hello ,${name}!");

}


Output

default.png

  • Final variable

             A final variable can be assigned only once; if you reassigned it, then it gives compile time error.

Example of final variable
 

void main() {
  final name="Google Dart";
 name='You are  trying to change a final variable';  //Error
  print("Hello It is a default value,${name}!");

}


Output

 errror.png


Ask Your Question

Got a programming related question? You may want to post your question here
 
© 2019 DotNetHeaven. All rights reserved.