How to create nullable variable in Dart programming language?
There are two ways to create nullable variable.
1) Using var keyword,
Syntax : var variable_name;
2) Using datatype
Syntax : int? variable_name;
- Nullable variable creation using var keyword.
void main() {
var a;
print(a);
a = "Hello world";
print(a);
}
Output:
null
Hello world
Explanation:
Here variable a
is nullable type. Variable a
can be null and any other datatype.
2. Nullable variable creation through type annotation.
void main() {
int? a;
print(a);
a = 45;
print(a);
}
Output:
null
45
Explanation:
Here variable a
is nullable type. Variable a
can be null and int type.