{

What is the difference between var and dynamic and final in dart?| Flutter By Example


This tutorial shows you the usage of var, dynamic and final keywords in the dart, see the between var vs dynamic vs final in the dart.

Difference between var, dynamic, and final in dart?

These three keywords are used to declare the variables in Dart. let’s see the difference between them.

  • var keyword var is used to declare and assign multiple values. Following are var keyword features

  • variable declared with var only change its value

  • datatype never be changed.

void main() {
  var variable = 123; // declared an int variable with value
  print(variable.runtimeType); // int

  variable=243; // can change it's value
  variable = "abc"; // compilation error to change its type, assigned with string
}
  • dynamic keyword

dynamic keyword is used to assign the dynamic types at runtime. Following are dynamic keyword features

  • variable declared with dynamic change its value
  • also, datatype can be changed
void main() {
  dynamic variable = 123; // declared an int variable with value
  print(variable.runtimeType); // int

  variable = 243; // can change it's value
  variable =
      "abc"; // Can change type, assigned with string
  print(variable.runtimeType); // String
}

void main() {
  final variable = 123; // declared an int variable with value
  print(variable.runtimeType); // int

  variable = 243; // compilation error , variable never be changed
  variable =
      "abc"; // compilation error to change its type, assigned with string

}
  • final keyword

The final keyword is used to assign the value only once. Also called compiled time constants. Following are final keyword features

  • variable declared with final never be changed its value
  • also, datatype never be changed

var vs dynamic vs final keywords in dart

Let’s see the comparison between var vs dynamic vs final keywords in dart

var dynamic final
used to declare a variable with assign multiple values at runtime used to declare variables to change type and value at runtime used to declare compile-time constants
Can change its value Can change its value Never be changed its value, once assigned with value
Datatype can never be changed Datatype can be changed datatype can never be changed
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Dart| Flutter How to: Find a Given String is numeric or not

How to iterate loop Enum in Dart | Get Enum with index in Flutter By Example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart tutorial examples/ Flutter By Examples

How to add Methods or Values to Enums in Dart | Flutter By Example

How to convert Double to Integer or Integer to double in Dart| Flutter By Example

How to: Enum to String in Dart | Print Enum name as String in Flutter By Example