]

Dart-variables and constants

Published May 07, 2020

Variables are 'namespaces in memory' and are used to store values. Variable names are called identifiers, and identifier naming rules:

  • Identifier cannot be a keyword
  • Identifier can contain numbers and letters
  • Identifiers cannot contain spaces and special characters, except the underscore (_) and dollar ($) symbols
  • Variable names cannot start with a number

grammar:

  • Using var to declare variables can assign different types of values
  • Variables without initial values ??will default to null
  • Use final to declare a variable that can only be assigned to one
  • Use const to declare a constant
  • Use const declaration must be a constant at compile time

 Syntax

A variable must be declared before it is used. Dart uses the var keyword to achieve the same

var a;

 

Example

var a;
  print(a);//result:null

  a = 20;//
  print(a);//result:20

  a = 'dart';
  print(a);//result:dart

  final b = 20;

//  b = 50;//final


  const c = 20;//
//  c = 50;
  print(c);

  var list = const[1,2,3,4];
  var obj = const Object();

 

dynamic,Object,var
Var and dynamic are completely two concepts and should not be put together for comparison. Var is actually syntactic sugar thrown by the compiler. Once compiled, the compiler will automatically match the actual type of the var variable and replace the variable with the actual type. The declaration is equivalent to that we used the actual type declaration when coding, and dynamic is compiled into the Object type, and the compiler does not type check dynamic when compiling
After the dynamic type is compiled, it is an object type.

What is the difference from object?
Compile-time type checking
Type conversion

 

Final and Const

The final and const keyword are used to declare constants. Dart prevents modifying the values of a variable declared using the final or const keyword. These keywords can be used in conjunction with the variable’s data type or instead of the var keyword.

The const keyword is used to represent a compile-time constant. Variables declared using the const keyword are implicitly final

The difference between final and const
final: indicates a single assignment, the final variable or field must be initialized, once initialized, the value of the final variable cannot be changed, and the final modified variable;
const: The const modified value can be used in creating collections and constructing objects, meaning that the entire depth state of the object can be completely determined at compile time, and the object is frozen completely immutable; variables declared with the const keyword are implicitly final , Only const variables can be used to calculate compile-time constants. Compile-time constants are constants whose value is determined at compile time

Example – const Keyword

void main() { 
   const pi = 3.14; 
   const area = pi*12*12; 
   print("The output is ${area}"); 
}

Output

The output is 452.15999999999997


Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

257 Views

Subscribe For Daily Updates

Flutter Questions
Android Questions