Today, we are going to discuss local, global, and dynamic variables along with learning late keyword & String interpolation concepts. There are two ways to write a variable i.e. by:-
- Local Variable
- Global Variable
Let’s learn both one by one:
Table of Contents
Local Variable
As we have learned in previous classes about writing variables in the body. Body means data in curly brackets, for example:-
void main() {
String name = “Ali”;
print(name);
}
So the above code written in the body is called a local variable. We cannot use it in another body.
Global Variables
If we write our code outside a body then it becomes a global variable and can be used in any body. It can be written at the start or end in Dart but it should not be in between a body otherwise it will become a local variable. Following is an example of a global variable:
int no = 40;
Conditions for Global Variable
We have to initialize while making a Global Variable because it does not work without value. Similarly, the print command also does not work in Global Variable. We need to use print command in a body like:
int no = 40;
void main() {
print(no);
}
So to resolve the issue of later initialization, we use a late modifier. For example:
late String a;
void main() {
a = “ali”;
print(a);
}
Late initialization
There is another benefit of late modifiers in local variables too. In that case, we will use it for lazy initialization. It is a memory-efficient approach and in this scenario, there is no burden of that variable on memory. In the following example we don’t need String instantly so we use late with it for lazy initialization:
void main() {
late String content = “It’s our prime responsibility to check the authenticity”;
int a = 20, b = 30;
int c = a * b;
print(c);
}
When to use a Late Keyword?
In short, we will use late keywords with variables for one of the following reasons:
-
Late initialization
When there will are ‘global’, ‘non-nullable’ & ‘uninitialized’ variables, we will have to use a ‘late’ modifier with those variables. Otherwise, we will get an error.
-
Lazy initialization
String literal concept
As you know we print after writing a variable. The text which we use in double quotes is called String literal. For example “It’s our prime responsibility to check the authenticity”. Similarly, we use terms e.g. integer literal for variables containing integer values and double literal, list, map, etc.
String interpolation
It is important to understand that we cannot use a variable in String literal. For this purpose, we use $ with a variable to show it in print. It is called String Interpolation for example:-
void main () {
String name = “Ali”;
print (“My name is $name”);
}
String interpolation with operation/ function
If we understand String Interpolation, it is easy to know that we can similarly use string literal expressions. We just need to remember its pattern which is as under:-
void main () {
int no1 = 45, no2 = 55;
print (“The sum of no1 & no2 is ${no1 + no2}”);
}
In curly brackets use any expression i.e. add, subtraction, multiplication, or division. But be sure to use the $ outside bracket instead inside.
Dynamic Variable
We have learned String, int, double, and bool types of variables so far. Dynamic is another type of variable that is similar to inferred variables. We use dynamic keywords for it. The difference between inferred variables and dynamic variables is we cannot change the data type in inferred variables whereas we can change the data type in dynamic. Let’s see its examples to understand:
In the case of inferred variable:
void main () {
var no2 = 30;
// can’t do var no2 = “Pakistan”
print (no2);
}
In the case of dynamic variables no issue:
void main (){
dynamic no2=45;
no2=”Pakistan”;
print (no2);
}
Try yourself in Dart Pad.
Hopefully, concepts related to Local, Global, Dynamic Variables, Late Keyword & String Interpolation would be clear. You must practice all the above stuff to become a master of it!!