In Dart, there are several different types of variables that you can use to store and manipulate data. The main types of variables in Dart are:
1. Numbers : Dart provides several types for representing numeric values, including integers and floating-point numbers.
* `int`: Represents integer values, such as
1, 2, -5,
etc.
* `double`: Represents floating-point numbers with decimal places, such as
3.14, -0.5,
etc.
2. Strings : Strings are used to represent sequences of characters. They are enclosed in single (`
'
`) or double (`
"
`) quotes.
* Example : `
'Hello, Dart!
'`, `
"FreeTimeLearn
"`
3. Booleans : Booleans represent logical values, either `
true
` or `
false
`. They are useful for making decisions and controlling program flow.
* Example : `
true
`, `
false
`
4. Lists : Lists are ordered collections of objects. They can contain elements of different types and can grow or shrink dynamically.
* Example : `
[1, 2, 3]
`, `
['apple', 'banana', 'orange']
`
5. Maps : Maps are key-value pairs where each key is associated with a value. They are also known as dictionaries or hash maps in other programming languages.
* Example : `
{'name': 'John', 'age': 25, 'country': 'USA'}
`
6. Sets : Sets are unordered collections of unique objects. They do not allow duplicate values.
* Example : `
{1, 2, 3, 4, 5}
`
7. Dynamic : The `
dynamic
` type represents a variable that can hold values of any type. The type is determined at runtime, allowing for flexibility but sacrificing some compile-time type checking.
* Example : `
dynamic dynamicVar = 10;
`, `
dynamicVar = 'Hello';
`
8. Var : The `
var
` keyword allows Dart to infer the type of a variable based on its assigned value. It's a shorthand way of declaring a variable without explicitly specifying the type.
* Example : `
var name = 'John';
`, `
var count = 5;
`
These are the main types of variables in Dart. By using these variable types, you can store and manipulate different kinds of data in your Dart programs.