Var keyword in dart

Var keyword in dart

ยท

1 min read

  • var is the keyword of the dart which has the unique work on it self we can not declare var as a variable or any other place.

  • var used to declare a variable and once it's assign they automatically knows the type of data based on the assigned to the variable because Dart is an infer type language.

  • we can declare only one type of the data type as same variable such as we we declare var a = 10 than a has int type and now we can not pass any string value or double value in a variable , if we try this than we can face the error.

  • Here, I am providing the example of the use of var keyword.

      void main() {
        var a = 40;
        print(a);
    
        a = "Dart";
        print(a); // Error: A value of type 'String' can't be assigned to a variable of type 'int'
      }
    
    • In this code we have already declare " a " as int type so in second time we can not pass string value on " a " , we can only store int value in " a " variable.
ย