In this section, we will cover the fundamental concepts of variables and data types in Dart. Understanding these basics is crucial as they form the foundation for more complex programming concepts.

What is a Variable?

A variable is a named storage that our programs can manipulate. Each variable in Dart has a specific type, which determines the size and layout of the variable's memory, the range of values that can be stored within that memory, and the set of operations that can be applied to the variable.

Declaring Variables

In Dart, you can declare a variable using the var, final, or const keywords. Here’s how you can declare variables:

void main() {
  var name = 'John'; // String type inferred
  final age = 30; // int type inferred, value cannot be changed
  const pi = 3.14; // double type inferred, compile-time constant
}
  • var: The type is inferred by the compiler based on the assigned value.
  • final: The variable can be set only once and is immutable.
  • const: The variable is a compile-time constant and is immutable.

Explicit Type Declaration

You can also explicitly declare the type of a variable:

void main() {
  String name = 'John';
  int age = 30;
  double pi = 3.14;
  bool isStudent = true;
}

Data Types

Dart is a statically typed language, which means that every variable must have a type. Here are the basic data types in Dart:

Numbers

  • int: Represents integer values.
  • double: Represents floating-point values.
void main() {
  int age = 30;
  double height = 5.9;
}

Strings

Strings are a sequence of characters. They are represented by the String type.

void main() {
  String greeting = 'Hello, World!';
}

Booleans

Booleans represent true or false values. They are represented by the bool type.

void main() {
  bool isLoggedIn = true;
}

Lists

Lists are ordered collections of objects. They are represented by the List type.

void main() {
  List<int> numbers = [1, 2, 3, 4, 5];
}

Maps

Maps are collections of key-value pairs. They are represented by the Map type.

void main() {
  Map<String, String> capitals = {
    'USA': 'Washington, D.C.',
    'France': 'Paris',
    'Japan': 'Tokyo'
  };
}

Practical Examples

Example 1: Basic Variable Declaration

void main() {
  var name = 'Alice';
  int age = 25;
  double salary = 50000.50;
  bool isEmployed = true;

  print('Name: $name');
  print('Age: $age');
  print('Salary: $salary');
  print('Employed: $isEmployed');
}

Example 2: Using Lists and Maps

void main() {
  List<String> fruits = ['Apple', 'Banana', 'Cherry'];
  Map<String, int> fruitPrices = {
    'Apple': 2,
    'Banana': 1,
    'Cherry': 3
  };

  print('Fruits: $fruits');
  print('Fruit Prices: $fruitPrices');
}

Exercises

Exercise 1: Variable Declaration

Declare variables of different types and print their values.

void main() {
  // Declare variables here
  var city = 'New York';
  int population = 8419000;
  double area = 468.9;
  bool isCapital = false;

  // Print the variables
  print('City: $city');
  print('Population: $population');
  print('Area: $area');
  print('Is Capital: $isCapital');
}

Exercise 2: Working with Lists and Maps

Create a list of your favorite movies and a map of movie titles with their release years. Print both the list and the map.

void main() {
  // Create a list of favorite movies
  List<String> favoriteMovies = ['Inception', 'The Matrix', 'Interstellar'];

  // Create a map of movie titles with their release years
  Map<String, int> movieReleaseYears = {
    'Inception': 2010,
    'The Matrix': 1999,
    'Interstellar': 2014
  };

  // Print the list and the map
  print('Favorite Movies: $favoriteMovies');
  print('Movie Release Years: $movieReleaseYears');
}

Common Mistakes and Tips

  • Type Mismatch: Ensure that the value assigned to a variable matches its declared type.
  • Immutable Variables: Remember that final and const variables cannot be reassigned.
  • Null Safety: Dart supports null safety, so be cautious of null values and use the ? operator when necessary.

Conclusion

In this section, we covered the basics of variables and data types in Dart. We learned how to declare variables, the different data types available, and how to use them in practical examples. Understanding these concepts is essential as they are the building blocks for more advanced topics in Dart programming. In the next section, we will explore operators in Dart.

© Copyright 2024. All rights reserved