What is Variable?
Variable is used to store the values, which can use further for operations.
We can think like we different size of glasses to store water or juice, same here we have different type and size of variables to store different kind of values.
Variables can be declare in multiple ways in dart
- var name = "Brain Mentors "; // Type Inference Way
- String name = "Brain Mentors"; // Statically Typed Way
- late String address; // Declare a Variable late and initalize later on
- String? name = "Amit"; // Null Safe String variable, this can be null.
Note: From Dart 2.12 added the late modifier, which has two use cases:
a) Declaring a non-nullable variable thatβs initialized after its declaration.
b) Lazily initializing a variable.
Example:
late String name;
void main() {
name = 'Amit Srivastava';
print(name);
}
Note: π If you fail to initialize a late variable, a runtime error occurs when the variable is used.
Following are the DataTypes provided by Dart.
Data Types can be Non Nullable or Nullable types from Dart 2.x
In type theory lingo, the Null type was treated as a subtype of all types:
Since Object is non-nullable now, it is no longer a top type. Null is not a subtype of it. Dart has no named top type. If you need a top type, you want Object?. Likewise, Null is no longer the bottom type. If it was, everything would still be nullable. Instead, Dart added a new bottom type named Never.
In Dart everything is object - Object have method's and property's
Example:
123.toString();
String name = "brain mentors";
name[0].toUpperCase();
name.substring(1).toLowerCase();
π After Declaring final it's time to understand about constant's in dart.
Use const for variables that you want to be compile-time constants. final can be use for compile time and runtime constant both.
const Value must be known at compile-time, const name Β = "Brain Mentors"
;
final Value must be known at run-time, final name = getCompanyNameFromDB()
;
String Type in Dart
String is a collection of characters. String is a predefine class in Dart . String can be declare in multiple ways in dart. following are the examples.
Comments in Dart
That's all Folks for this tutorial, see you in next tutorial. Happy Coding π.