In this article, you are going to get a very simplified and clear knowledge of what a variable is in Dart programming language. I promise, you will love it.
What is a variable?
In its simplest form, a variable is a container that holds or stores a certain type of value. E.g string, integer, boolean etc.
It does not literally store a value, but stores a reference to that value.
String firstName = 'john';
From the above code snippet, the variable name called ‘firstName’ contains a reference to a String object that holds a value of ‘john’. In dart, everything is an object.
Dart can also infer the type of the value if the type isn’t specified. for example;
var firstName = 'john';
From the above snippet, dart is able to detect the variable type of the value as String even though not specified, this is called Type Inference. Dart can infer a variable type only if it is initialized.
If a variable object isn’t restricted to a single type, you can specify the type as Object or dynamic if necessary.
Object firstName = 'john';
dynamic age = 23;
Null Safety
Since Dart 2.12, null safety was introduced to help prevent an error that results from unknowingly accessing variables set to null. The error is known as null dereference error. It occurs when you access the property or call a method on an expression that evaluates to null.
You can control whether a variable should be nullable by adding ‘?’ to the end of its type declaration.
String? firstName; // This can either be null or of type String
String firstName; // Non-nullable, cannot be null but can be of type String
Null safety flags a non-null variable;
- when it has not been initialized with a non-null value
- when it is assigned a null value
A nullable variable has an initial value of null by default, but for a non-nullable variable, you will need to assign it a non-null value.
Note: You don’t need to initialize a local variable even if it’s non-null, but you need to assign it a value before it is used.
Late Variables
If you are sure a variable will be initialized after its declaration and before usage, you can add the late modifier before the type declaration.
late String name;
void main(){
name = 'Chibuzor';
print(name);
}
From the above code, since the initState()
method is the first method that runs when a widget starts, it is advisable to do all initial configurations needed by the widget. It is the best place to initialize the late variable before it is used.
Final and Const
Declare a variable either ‘final’ or ‘const’ if you don’t intend for it to change. Final should be used when you are not sure of what the value will be at the start of your application. For example, when you want set a class constructor’s properties, setting them as final is suitable because you don’t know the exact value that will be received when the code is ran.
class User{
final String name;
User({required this.name});
void setName(){
print(name);
name = 'Kennedy'; // error: final variable can only be set once
}
}
Const should be used when the value of the variable is constant, meaning the exact value is known and will not change at anytime within the program. Const variable cannot be assigned a value after initialization.
const String name = 'Chibuzor';
name = 'Kennedy'; // error: constant variables can't be assigned a value after initialization
The value of a ‘final’ variable is known at runtime while that of a ‘const’ at compile time.
If you intend for a variable to change, you can use the ‘var’ keyword before the variable name. Note, you can’t specify ‘var’ alongside a type.
var String name = 'Chibuzor'; // This won't work
var name = 'Chibuzor'; // This will work
name = 'Kennedy'; // This will work
Kindly note — for a data type like List, if marked as final, it can be modified but not reassigned. But if marked as const, it can neither be modified nor reassigned.
final myList = [2, 4];
myList.add(6); // This will work
myList = [1, 7, 8]; // This won't work
const myList = [2, 4];
myList.add(6); // This won't work
myList = [1, 7, 8]; // This won't work
Conclusion
Variable is a core component of a program. Understanding its dynamics is very important. I hope I’ve been able to demystify any conundrum hinged on the subject of variables.
Happy Coding </>
Top comments (0)