Variables and Data types
In Dart, variables are used to store data values. Each variable has a specific data type, which determines the kind of values it can hold.
Dart is a statically typed language, which means that variables must be declared with their data types before they can be used.
You are familiar with the topic ? Check your knowlege
Rules for declaring variable in dart
There are certain guidelines we need to follow while declaring the varibales. They are:
_
.alphanumeric
characters and underscores (A-z, 0-9, and _ )
.case-sensitive
(age
, Age
and AGE
are three different variables).In Dart, variables can be declared using the var
keyword followed by the variable name and an optional initial value. Here’s an example:
Example
var name = 'John';
In the above example, the variable name
is declared using the var
keyword. The variable name is name
and the initial value is 'John'
. The variable name
is of type String
because the initial value is a string.
The var
keyword is optional. You can also declare variables using the data type of the initial value. Here’s an example:
String name = 'John';
In the above example, the variable name
is declared using the String
data type. The variable name is name
and the initial value is 'John'
. The variable name
is of type String
because the initial value is a string.
var keyword will understand the datatype itself after the value is assigned. This means variables declared using values assigned will convert to var to the datatype of value.
In Dart, variables can be assigned new values using the assignment operator =
. Here’s an example:
Example
var name = 'John';
name = 'Jane';
Output
Jane
In the above example, the variable name
is assigned the value 'John'
and then the value 'Jane'
.
In dart, there are two types of varibales :
Type | Scope |
---|---|
public | When you define a varibale by default is public |
private | if you define a variable with the underscrore prefix (_ ) then its a private variables. Scopes of private variables are limited to a file or class |
Public Variables
int name = "Flutter World";
Private variables
int _name = "Flutter World";
In Dart, variables can be declared using the following data types:
We will discuss more about List, Map and Set in the Dart collections chapter.
In Dart, the int data type is used to represent integer values. Here’s an example:
Example
int age = 20;
In the above example, the variable age
is declared using the int
data type.
The variable name is age
and the initial value is 20
. The variable age
is of type int
because the initial value is an integer.
In Dart, the double data type is used to represent floating-point values. Here’s an example:
Example
double pi = 3.14;
In the above example, the variable pi
is declared using the double data type.
The variable name is pi
and the initial value is 3.14
. The variable pi
is of type double because the initial value is a floating-point(decimal) value.
In Dart, the num data type is used to represent numeric values. Here’s an example:
Example
num pi = 3.14;
In the above example, the variable pi
is declared using the num data type. The variable name is pi
and the initial value is 3.14
. The variable pi
is of type num because the initial value is a numeric value.
num can store both int and double values.
In Dart, the String data type is used to represent string values. Here’s an example:
Example
String name = 'John';
In the above example, the variable name
is declared using the String data type. The variable name is name
and the initial value is 'John'
. The variable name
is of type String because the initial value is a string value.
Manipulation of string means to change the string in some way. Dart supports the following string manipulation methods:
Concatenation is the process of joining two or more strings together. Dart supports the + operator for concatenation.
Syntax
string1 + string2
Example
void main() {
String str1 = "Hello";
String str2 = "World";
print(str1 + str2);
}
Output
HelloWorld
Interpolation is the process of inserting the value of an expression or variable into a string. Dart supports the $ operator for interpolation.
Syntax
string1 $expression string2
Example
void main() {
String str1 = "Hello";
String str2 = "World";
print("$str1 $str2");
}
Output
Hello World
In Dart, the bool data type is used to represent boolean values. Here’s an example:
Example
bool isMarried = false;
In the above example, the variable isMarried
is declared using the bool data type. The variable name is isMarried
and the initial value is false
. The variable isMarried
is of type bool because the initial value is a boolean value.
Boolean variables can hold true or false only
In Dart, the dynamic data type is used to represent dynamic values. Here’s an example:
Example
dynamic age = 20;
In the above example, the variable age
is declared using the dynamic data type. The variable name is age
and the initial value is 20
.
The variable age
is of type dynamic because the initial value is a dynamic value.
In Dart, the var
keyword can be used to declare variables without specifying their data types. Here’s an example:
Example
var name = 'John';
In the above example, the variable name
is declared using the var
keyword. The variable name is name
and the initial value is 'John'
. The variable name
is of type String
because the initial value is a string.
In Dart, the var
keyword can also be used to declare variables without specifying their data types. Here’s an example:
var age = 20;
In the above example, the variable age
is declared using the var
keyword. The variable name is age
and the initial value is 20
. The variable age
is of type int
because the initial value is an integer.
Check your knowlege with the quiz below.