Data types are essential in programming, defining the kind of data that can be stored and manipulated within a program. Dart offers a rich set of data types, which can be categorized into built-in types and user-defined types.
Built-in Data Types
Dart provides several built-in data types that you can use to store different kinds of values:
String
- Represents a sequence of characters (text).
- Strings can be enclosed in single or double quotes.
Example:
String greeting = 'Hello, Dart!';
int
- Represents integer values (whole numbers) without a decimal point.
- It can hold positive and negative numbers.
Example:
int age = 25;
double
- Represents floating-point numbers (numbers with decimal points).
- It can store both positive and negative decimal values.
Example:
double height = 5.9;
num
- A superclass for both
int
anddouble
, allowing you to store any numeric value. - Useful when you want to handle both integer and floating-point numbers interchangeably.
Example:
num score = 95.5; // Can also be an int, e.g., num score = 100;
- A superclass for both
bool
- Represents boolean values, which can be either
true
orfalse
. - Commonly used for conditional statements.
Example:
bool isPassed = true;
- Represents boolean values, which can be either
List
- An ordered collection of items, which can be of any data type.
- Lists in Dart can be mutable or immutable.
Example:
List<String> fruits = ['Apple', 'Banana', 'Cherry'];
Map
- A collection of key-value pairs, where each key is unique.
- Useful for storing related data.
Example:
Map<String, int> scores = {'Alice': 90, 'Bob': 85};
Set
- An unordered collection of unique items.
- Useful for storing distinct elements.
Example:
Set<String> colors = {'Red', 'Green', 'Blue'};
User-Defined Data Types
In addition to built-in data types, Dart allows you to define your own types using classes and enums.
Classes
- You can create classes to define complex data structures with properties and methods.
Example:
class Person { String name; int age; Person(this.name, this.age); } void main() { var person = Person('John', 30); print('Name: ${person.name}, Age: ${person.age}'); }
Enums
- Enums are used to define a fixed set of constants.
- Useful for representing a group of related values.
Example:
enum Color { red, green, blue } void main() { var favoriteColor = Color.green; print('Favorite Color: $favoriteColor'); }
Type Inference
Dart has a powerful type inference system, which allows you to declare variables without explicitly specifying their types. The Dart compiler can infer the type based on the assigned value.
Example:
var temperature = 36.5; // Dart infers that temperature is of type double
Type Conversion
Dart allows you to convert between different data types, which can be necessary in various scenarios. Here are common type conversions:
Converting String to int or double:
- Use
int.parse()
ordouble.parse()
for converting a string to a number.
Example:
String ageString = '30'; int age = int.parse(ageString); // Converts String to int double height = double.parse('5.9'); // Converts String to double
- Use
Converting int or double to String:
- Use the
toString()
method.
Example:
int score = 95; String scoreString = score.toString(); // Converts int to String
- Use the
Summary of Data Types in Dart
Data Type | Description | Example |
---|---|---|
String | Sequence of characters | String name = 'Alice'; |
int | Whole numbers (integer values) | int age = 30; |
double | Floating-point numbers | double height = 5.9; |
num | Can hold both int and double | num score = 95.5; |
bool | Boolean values: true or false | bool isActive = true; |
List | Ordered collection of items | List<String> fruits = []; |
Map | Key-value pairs | Map<String, int> scores = {}; |
Set | Unordered collection of unique items | Set<String> colors = {}; |
Class | User-defined type with properties and methods | class Car {} |
Enum | Fixed set of constants | enum Direction { north, south } |
Conclusion
Understanding data types in Dart is crucial for effective programming. Knowing how to use built-in types, create user-defined types, leverage type inference, and perform type conversions will enhance your ability to write efficient and clear code. This foundational knowledge will serve you well as you advance in your Dart programming journey and tackle more complex applications. For further learning, refer to the next sections in the outline.
PLAY QUIZ