Var, Final And Dynamic Keywords In Flutter 😎

Introduction

Hi folks, in the previous article we discussed 3 flutter variables. Now we learn about more variables in dart.  In this article, we will learn about var, final and dynamic keywords it’s more important in dart. So let’s start.

Var Keyword

Var keyword is used to create any variable in any type we don't need a specific variable type for example in the previous section we discussed the string concept if you don't know how to declare please check my previous article. Ok, get back to this topic Normally declare a sequence of characters using  String name = ‘CSharp Corner’ But in var case, we only declare values without specifying any type. For example var name = ‘CSharp Corner’ 

More Detail

If you assign any value in var you can’t change different types of values in later code. Please see the below example.

Var Keyword in Dart

We declare string using the var keyword. Please check the console it shows normal string output. 

Var Keyword in Dart

In the above image, I assign a number to the same name as the previous declare one it throws an error like A value of type 'int' can't be assigned to a variable of type 'String'.

If you declare an uninitialized var you can change the value of var in your later code. Please see the below example.

Var Keyword in Dart

Dynamic Keyword

If you create a variable using a dynamic keyword the data type assign automatically based on what variable you declared. For example, if your value is a string the dynamic keyword automatically detects the string data type.

More Detail

  • The dynamic variable can change the type of the variable. Please see the below image. I defined the var keyword later I update var but I already discussed it throwing errors like in the previous section. 
  • But the dynamic value was first defined as a string data type and I change the dynamic name string to integer suddenly but the program doesn’t throw any error.

Dynamic Keyword in Dart

Final Keyword

This final keyword is simple once assign a value using the final keyword you cannot change the data type and cannot change the value of its data type. 

More Detail

  • Variable assigned once a runtime
  • They are immutable
  • Used to create instance variables for a class.

Final in Dart

Const

In Dart, the const keyword has the same behaviour as the final keyword. The sole distinction between final and const is that the variable becomes constant only at compilation time. When an object has const applied to it, its whole deep state is fixed rigidly at compilation time, rendering it totally immutable and frozen. In Dart, the const keyword has the same behaviour as the final keyword. The sole distinction between final and const is that the variable becomes constant only at compilation time. When an object has const applied to it, its whole deep state is fixed rigidly at compilation time, rendering it totally immutable and frozen.

Const in Dart

Conclusion

In this article, we discussed more important topics in flutter 3, I hope I explain clearly about var, final and dynamic. If you have any doubts or content mistakes please comment. In the next article, we will discuss functions and deep dive into them. Thank you