Dart has some basic variables types that are available for you to use.
- Numbers – which can be further separated into integers and floating-point numbers
- Strings
- Booleans
Inferred Definitions
Dart doesn’t require you to define the data type when you define a variable. You can use the var
keyword and Dart will infer the data type based upon what it sees.
var sampleVariable = 1.0 // infer type double
var fulleName = "Bob Smith"; // infer a string data type
Implicit Definitions
Likewise, you can define the data type when you define the variable if you want.
double sampleVariable = 1.0 // implicit type double
String fulleName = "Bob Smith"; // implicit a string data type
Nullable Variables
Variables in Dart cannot normally hold a null value. However, when you define an implicit variable, you can allow it to store a null value. The null value is a missing piece of data. This could be if you are loading data from an external source, and maybe it isn’t there.
If you don’t specify it as nullable, then you will get an exception, so if you cannot guarantee a piece of data is going to exist, you might want to make it nullable.
To make a piece of data nullable, add a question mark ?
after the data type name like you see below.
String? nick_name;
Basic Variables in Dart was originally found on Access 2 Learn