3  - Variables ( Mastering dart 2024 )

3 - Variables ( Mastering dart 2024 )

Introduction to Variables in Dart

Overview of Variables

  • The lesson introduces the concept of variables in Dart, emphasizing a detailed explanation compared to previous discussions.
  • A variable is described as a name that points to values stored in memory, allowing for dynamic data storage.

Types of Variables

  • The instructor outlines different types of variables: String, Integer, Double, Number (abbreviated as Num), and Boolean.
  • Emphasis on using var for dynamic typing, which can accept any value type without causing errors during execution.

Understanding Data Types

String vs. Numeric Types

  • Strings must be enclosed in double or single quotes; numbers should not be quoted when defined as integers.
  • The distinction between Integer and Double is clarified; Integers are whole numbers while Doubles can include decimal points.

Dynamic Type Handling

  • The num type encompasses both Integer and Double, allowing flexibility in assigning numeric values.
  • Boolean variables can only hold two values: true or false. This is illustrated with an example about marital status.

Naming Conventions and Restrictions

Variable Naming Rules

  • Variable names cannot contain spaces; they must be continuous words or use camelCase for readability.
  • Keywords like int, String, etc., cannot be used as variable names due to reserved status in Dart.

Special Characters and Starting Letters

  • Variable names cannot start with a number but may end with one. Special characters are generally prohibited except for $ and _.

Mutable vs Immutable Variables

Mutability Explained

  • Mutable variables allow changes after their initial assignment; immutable ones do not permit reassignment once set.

Using const for Constants

  • To declare an immutable variable, the keyword const is used. An example includes defining constants like mathematical constants (e.g., π).

Best Practices in Variable Declaration

Code Readability Tips

  • For multi-word variable names, using camelCase improves readability (e.g., firstName instead of firstname).

Conclusion

  • The lesson wraps up by encouraging viewers to apply these principles when coding in Dart for better clarity and efficiency.