Variables
Variables are containers used to store value in the program. There are different types of variables where
you can keep different kinds of values. Here is an example of creating a variable and initializing it.
String name = 'Shahid';
Syntax:
Syntax defines how to write code that a programming language can understand and execute. It
encompasses the following aspects:
Syntax is essential in programming, as it helps ensure that code is:
- Unambiguous
- Consistent
- Easy to read and understand
- Free from errors
Syntax
This is syntax for creating a variable in dart is very simple
type variableName = value;
String name = 'ali';
Note: Always use the descriptive variable name. Don’t use a variable name like
a, b, c because this will make your code more complex.
Rules For Creating Variables In Dart
Variable names are case sensitive, i.e., a and A are different.
A variable name can consist of letters and alphabets.
A variable name cannot start with a number.
Keywords are not allowed to be used as a variable name.
Blank spaces are not allowed in a variable name.
Special characters are not allowed except for the underscore (_) and the dollar ($)
sign.
Dart Constant
Constant is the type of variable whose value never changes.
In programming, changeable values are mutable and unchangeable values are
immutable
. Sometimes, you don’t need to change the value once declared. Like the value of
PI=3.14, it never changes.
To create a constant in Dart, you can use the const keyword.
Syntax of declaring Constant
const type nameOfConstant = value;
Naming Convention(Rules) For Variables In Dart
It is a good habit to follow the naming convention.
In Dart Variables, the variable name should start with lower-case, and every
second word’s first letter will be upper-case like num1, fullName, isMarried, etc.
Technically, this naming convention is called lowerCamelCase.
Naming Convention Example
// Not standard way it is good but not according to dart language conversation
var fullname = "John Doe";
// Standard way recommended by seniors developers
Because it increase code readability
var fullName = "John Doe";
const pi = 3.14;
Data Types
Data types help you to categorize all the different types of data you use in your
code.
For e.g. numbers, texts, symbols, etc.
The data type specifies what type of value will be stored by the variable. Each
variable has its data type.
Dart supports the following built-in data types :
1) Numbers
2) Strings
3) Booleans
4) Lists
5) Maps
6) Sets
7) Runes
8) Null
DATA TYPES IN DART
Create Free Backend With Appwrite
Data Types
Data types help you to categorize all the different types of data you use in your
code. For e.g. numbers, texts, symbols, etc. The data type specifies what type of
value will be stored by the variable. Each variable has its data type. Dart supports
the following built-in data types :
Numbers
Strings
Booleans
Lists
Maps
Sets
Runes
Null
Built-In Types
In Dart language, there is the type of values that can be represented and
manipulated. The data type classification is as given below:
Data Type. Keyword Description
Numbers. int, double, num It represents numeric values
Strings String It represents a sequence of characters
Booleans bool It represents Boolean values true and false
Lists List It is an ordered group of items
Maps Map It represents a set of values as key-value pairs
Sets Set It is an unordered list of unique values of same types
Runesrunes It represents Unicode values of String
Null null It represents null value
Var Keyword In Dart
In Dart, var automatically finds a data type. In simple terms, var says if you don’t
want to specify a data type, I will find a data type for you.
void main(){
var name = "Ali"; // String
var age = 20; // int
print(name);
print(age);
Final vs Const keyword in dart
Use final when you need variables that cannot be reassigned but can be
computed at runtime.
Use const for values that are known at compile time to enhance performance
and ensure immutable behavior.
Statically Typed language
A language is statically typed if the data type of variables is known at compile
time. Its main advantage is that the compiler can quickly check the issues and
detect bugs.
Dynamically Typed language
A language is dynamically typed if the data type of variables is known at run time.
Type safety in Dart refers to the guarantee that a variable will always hold a value
of the specified type, preventing type-related errors at runtime.
In Dart, type safety is achieved through static typing, which means that the data
type of a variable is known at compile time.
Example:
```
int myVariable = 42; // myVariable is of type int
myVariable = 'hello'; // Error: Type 'String' is not assignable to type 'int'
```
In this example, the variable `myVariable` is declared as an `int`, and attempting
to assign a `String` value to it results in a compile-time error.
Dart's type system ensures that:
- A variable can only hold a value of its declared type.
- A function can only return a value of its declared return type.
- A class can only have properties and methods of their declared types.
Type safety benefits include:
- Fewer runtime errors
- Better code completion and debugging
- Improved code readability and maintainability