Declaration and Initialization of Variables
The declaration means naming the variable and giving the proper data type to it, but dart being type infer language, it can automatically know the data type. In contrast, initialization means initializing the variable with some value of the appropriate data type.
Code:
void main() {
//Declaring the variable.
String s;
//Initializing the variable.
s = "Article on variables in dart";
print("s: $s");
}
Output:
s: Article on variables in dart
A Declaration of a variable must be made before it is used. We declare a variable using three ways:
1. Using var keyword, since dart is a type infer language, it can automatically infer the type of data we are assigning to it at first and make the variable of that data type.
Code:
void main() {
// Creating a variable of type var.
var s = "Coding Ninjas";
// Printing the data type of 's' after assigning a value to it.
print(s.runtimeType);
}
Output:
String
Code:
void main() {
// Assinging a value to var type variable.
var a = "Coding Ninjas";
// Assigning different data type value.
a = 100;
}
Output:
Error compiling to JavaScript:
Info: Compiling with sound null safety
Warning: Interpreting this as package URI, 'package:dartpad_sample/main.dart'.
lib/main.dart:3:7:
Error: A value of type 'int' can't be assigned to a variable of type 'String'.
a = 100;
^
Error: Compilation failed.
2. Explicitly define the data type before its name.
Code:
void main() {
// Creating a variable of string type.
String s = "Coding Ninjas";
// Creating a variable of int type.
int a = 100;
// Creating a variable of double type.
double b = 10.5;
print(s.runtimeType);
print(a.runtimeType);
print(b.runtimeType);
}
Output:
String
int
double
3. Using the dynamic keyword, we can change the value of the variable as well as its data type also. Unlike in the case of var, we can't change its data type.
Code:
void main() {
// Creating a dynamic variable.
dynamic a = "Coding Ninjas";
print(a.runtimeType);
// Changing the value of variable.
a = 100;
print(a.runtimeType);
}
Output:
String
int
in
Final and Const
The final and const keywords are used during the declaration of a variable to specify the program and whether its value can be changed or not.
The final keyword is used to specify that the value of a variable cannot be changed at any line of the program.
Code:
void main() {
// Creating a variable of final type.
final String s = "Coding Ninjas";
// Changing value of final variable.
s = "Coding";
}
Output:
Error compiling to JavaScript:
Info: Compiling with sound null safety
Warning: Interpreting this as package URI, 'package:dartpad_sample/main.dart'.
lib/main.dart:3:3:
Error: Can't assign to the final variable 's'.
s = "Coding";
^
Error: Compilation failed.
The const keyword does exactly the same thing as the final does, but it just specifies at the compile time that its value cannot be changed.
Code:
void main() {
// Creating a variable of const type.
const String s = "Coding Ninjas";
// Creating a variable of const type.
s = "Coding";
}
Output:
Error compiling to JavaScript:
Info: Compiling with sound null safety
Warning: Interpreting this as package URI, 'package:dartpad_sample/main.dart'.
lib/main.dart:3:3:
Error: Can't assign to the const variable 's'.
s = "Coding";
^
Error: Compilation failed.
Code:
void main() {
// Creating a const variable for pi.
const double pi = 3.14;
int radius = 10;
// Calculating the area of circle.
double area = pi*radius*radius;
print(area);
}
Output:
314
FAQs
How is dart language helpful in making apps?
Actually, we use flutter to make hybrid apps, and flutter is the framework of dart language. All the functionality of the flutter is based on the dart language. This means, that all the functions that we write will going to be in the dart.
What is the meaning of dynamic data types?
It means we can declare a variable and assign values of any data type to it. Dynamic is different from var because in var when we assign a value to a variable, it will become of that data type. Using dynamic keyword you can assign data of different data types to the variable.
Can we use const and final with non-explicit variables?
Yes, we can use these keywords before the keyword var while using non-explicit variables.
Conclusion
In this article, we've thoroughly discussed the Variables in dart and the keywords we write to use them. We discussed how different types of variables could be declared and initialized.
We hope that this blog helped you enrich your knowledge regarding the Variables in Dart. We can use the concepts of dart in building an android app to learn in-depth about android development. Check out our Android Development course on the Coding Ninjas website. Do upvote our blog to help other ninjas grow.
Happy Coding!