var and dynamic keyword

 ///"var" keyword : One of many ways, and the simplest way, to define a variable in Dart is using the var key word.


void main() {
///One of many ways, and the simplest way, to define a variable in Dart is using the "var" keyword.
var message = "Hello World"; //Here, variable "message" will be of String Data type value
///This example creates a variable called message and also initializes the variable with a String value of "Hello World".

//message = 21.90; //Not Allowed
//message = 7; //Not Allowed

message = "Next Time"; //Allowed, Variable value will be Override with same data Type value(In Declaration time)
print(message);


///Dart is a typed language. The type of the variable message is String. Dart can infer this type,
///so you didn't have to explicitly define it as a String. Importantly, this variable must be a String forever.
///You cannot re-assign the variable as an integer. If you did want to create a variable that's more dynamic,
///you'd use the dynamic keyword.


///"dynamic" keyword:
dynamic v1 = "How are you ?";

v1 = 33.89; //Allowed, You can safely re-assign this variable to an integer.
v1 = "abc"; //In case of "dynamic", variable values will be Override
v1 = 5; //variable value will be Override
print(v1);

///It's rarely advisable to use dynamic. Your code will benefit from being type safe.
}

Comments

Popular posts from this blog

Second GET API Calling with Bloc simple Example in Flutter

Stack Container Scrollable Card widget UI with Custom Widget

Pagination with Bloc Pattern in Flutter