2

I am learning dart and this behavior of dart is annoying me

var a=10;
a="John";    //it generates an error, because a has integer data-type and we can't assign 
             //string or the value of any other data type because we have assigned an 
             //integer value

but at the same time, dart allows us to write

var a;
a=10;
a="John";
print (a)     //it displays John

it means that we can't assign the value of any other data type when a variable initializes at the time of declaration but we can assign the value of any other data type if the variable declares in one line and initializes at the second line. Why does dart work in this way?

Muhammad Afzal
  • 118
  • 1
  • 9

2 Answers2

3

The var keyword is used to let Dart automatically assign a type for the variable based on the value you are using to initialize the variable.

If you use var without providing any initial value, the type of your variable are automatically being assign to dynamic which allows any type of object to be assigned to the variable. But it also means your program are much less type safe since the analyzer can no longer help you with what type the variable are going to return and what types are allowed when setting the variable.

So in your first example, a is being assigned the type int since you provide 10 as the initial value. It is therefore a compile error when you are trying to set a String as the new value of your int variable.

In your second example, a is going to be dynamic since you are not providing any initial value. So it is not a problem to first give it 10 and later 'John' since dynamic allow us to use any type. But that also means that when we try to use a in our program, Dart cannot make any guarantees about what type of object you are going to get so errors will first be identified at runtime.

julemand101
  • 28,470
  • 5
  • 52
  • 48
  • You said **the type of your variable are automatically being assigned to dynamic** but when we do not initialize a variable then it doesn't belong to `dynamic` instead it belongs to Null class because null is assigned as a default value ` var a; print(a.runtimeType); //it displays Null ` So, what actually `dynamic` is? is it just a term? – Muhammad Afzal Jan 15 '22 at 02:43
  • @MuhammadAfzal Correct. But since `var a;` would be kinda useless if it got assigned the type `Null`, Dart does instead make it `dynamic` in this specific case. `dynamic` means `Object?` (because that is the least we know about the type) but where Dart will not complain when you try call/access something on `a` since `dynamic` signals that the statically type system should not complain since you, the programmer, promise to know what you are doing is going to work at runtime. :) – julemand101 Jan 15 '22 at 08:36
  • I should add that if you don't want these "sneaky" cases where something is implicit being typed `dynamic`, you can disable that feature in the Analyzer: https://dart.dev/guides/language/analysis-options#enabling-additional-type-checks . Personally, I always runs with both `implicit-casts` and `implicit-dynamic` disabled since I don't really like this kind of "surprises". If you disable `implicit-dynamic`, you can no longer just write `var a;` since you will get a "Missing variable type for 'a'." error from the analyzer. `var a = 5;` is still valid since we initialize the variable with a value – julemand101 Jan 15 '22 at 08:38
  • thank you for the nice explanation, I definitely disable implicit-dynamic – Muhammad Afzal Jan 19 '22 at 09:10
2

That's because var means dart will assign the type of variable itself.

In first case, the dart assigned variable a as int type because it was given a value during initialisation.

But in second case, the dart assigned variable a as dynamic and that's why you can assign it either a string or int later on.

Muhammad Hussain
  • 966
  • 4
  • 15
  • i just check the type of `a` and it's `Null`, so what do you mean by **the dart assigned variable `a` as dynamic** – Muhammad Afzal Jan 19 '22 at 09:11
  • 1
    Yes indeed, in the beginning the runTimeType of `a` was `Null` but later the runTimeType changed to `int` and then `String`, and these runTimeType was allowed to be changed by dart just because it was overall assigned as dynamic by compiler during initialisation. Check this out. https://stackoverflow.com/questions/12416507/difference-between-var-and-dynamic-type-in-dart – Muhammad Hussain Jan 19 '22 at 12:14
  • 1
    thank you so much, the link you shared helped me a lot. dynamic is a type as others int or double but the difference is the variables declared with dynamic can change their type even after being assigned value to them. dynamic a; a=123; a="ABC". And when we don't assign a value to a variable using var keyword like var a; then it behaves the same like dynamic and it's also important to know that dynamic underlying all dart objects. – – Muhammad Afzal Jan 20 '22 at 07:39