1  Variables

Variables are one of the fundamental building blocks in any programming language. In Dart, variables have some unique characteristics that combine elements you might recognize from both Python and statically-typed languages like Java. Let’s explore Dart variables thoroughly.

1.1 Variable Declaration

In Dart, you can declare variables in several ways:

// Using var (type inference)
var name = 'John';

// Explicit type declaration
String lastName = 'Doe';

// Using dynamic type (similar to Python's dynamic typing)
dynamic something = 'Hello';
something = 42; // This is valid, can change types

// Using final (similar to const in other languages)
final int age = 30;

// Using const (compile-time constant)
const double PI = 3.14159;

The var keyword in Dart is particularly interesting. Unlike Python, which is dynamically typed, when you use var in Dart, the variable’s type is inferred at compile time and then fixed. This gives you the convenience of Python-like syntax with the safety of static typing.

1.2 Understanding Dart’s Type System

Coming from Python, you’ll notice that Dart is statically typed, which means the type of a variable is determined at compile time. This helps catch errors before you run your program.

var name = 'John'; // Type inferred as String
name = 42; // Error! Can't assign an int to a String variable

However, if you explicitly want a variable that can change types (like in Python), you can use dynamic:

dynamic flexible = 'Hello';
flexible = 42; // This works fine
flexible = true; // Also works

1.3 Built-in Data Types

Dart offers several built-in data types:

1.3.1 Numbers

// Integers
int count = 42;
var negativeNumber = -10;

// Doubles (floating-point)
double price = 9.99;
var pi = 3.14159;

// Dart also has a num type, which can be either int or double
num anyNumber = 42;
anyNumber = 3.14; // This is valid

1.3.2 Strings

// String literals can use single or double quotes
String name = 'John';
var greeting = "Hello";

// String interpolation (similar to f-strings in Python)
var message = "Hello, $name!"; // "Hello, John!"

// For expressions, use curly braces
var result = "2 + 2 = ${2 + 2}"; // "2 + 2 = 4"

// Multi-line strings
var paragraph = '''
This is a multi-line
string in Dart.
''';

1.3.3 Booleans

bool isActive = true;
var isCompleted = false;

1.3.4 Lists (similar to Python lists)

// List declaration
List<String> fruits = ['apple', 'banana', 'orange'];
var numbers = [1, 2, 3, 4, 5];

// Accessing elements
print(fruits[0]); // apple

// Adding elements
fruits.add('grape');

1.3.5 Maps (similar to Python dictionaries)

// Map declaration
Map<String, int> ages = {
  'John': 30,
  'Jane': 25,
  'Bob': 40,
};

var scores = {
  'math': 90,
  'science': 85,
  'history': 95,
};

// Accessing elements
print(ages['John']); // 30

// Adding elements
ages['Alice'] = 35;

1.4 Final vs Const

Dart offers two ways to make variables immutable:

// final variables can be set only once
// They're runtime constants
final String name = 'John';
// name = 'Jane'; // Error!

// const variables are compile-time constants
// They must be initialized with a constant value
const double PI = 3.14159;
// const currentTime = DateTime.now(); // Error! Not a constant

The key difference is that final variables are evaluated at runtime, while const variables must be known at compile time.

1.5 Null Safety

Since Dart 2.12, Dart has null safety, which helps prevent null reference errors.

// Non-nullable variable
String name = 'John';
// name = null; // Error!

// Nullable variable (can be null)
String? nullableName = 'John';
nullableName = null; // This is valid

// Late initialization
late String lastName;
// Use the variable later
lastName = 'Doe'; // This is valid

The ? after a type indicates that the variable can be null.

1.6 Comparing with Python

Let’s see how Dart variables compare with Python:

# Python (dynamically typed)
name = "John"  # str type
name = 42      # int type - Python allows type changes

# Dart (statically typed with type inference)
var name = "John";  // String type
// name = 42;      // Error! Can't change type

Dart’s approach combines the convenience of Python’s syntax with the safety of static typing, which can help catch many errors at compile time rather than runtime.

1.7 Practical Example in Flutter Context

Let’s see how variables are used in a real Flutter widget:

class CounterWidget extends StatefulWidget {
  @override
  _CounterWidgetState createState() => _CounterWidgetState();
}

class _CounterWidgetState extends State<CounterWidget> {
  // Instance variables in a class
  int _counter = 0;
  final String _title = 'Counter App';
  
  // A method to increment the counter
  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }
  
  @override
  Widget build(BuildContext context) {
    // Local variables in a method
    var buttonColor = Colors.blue;
    String displayText = 'Count: $_counter';
    
    return Scaffold(
      appBar: AppBar(title: Text(_title)),
      body: Center(child: Text(displayText)),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        backgroundColor: buttonColor,
        child: Icon(Icons.add),
      ),
    );
  }
}

In this example, we use different types of variables: - Instance variables (_counter, _title) - Local variables in methods (buttonColor, displayText) - We use string interpolation to display the counter value

1.8 Exercise for Practice

Let’s create a simple exercise to practice variables in Dart. Try to create a new Dart file (e.g., variables_practice.dart) with the following tasks:

  1. Create variables for your name, age, and favorite programming languages
  2. Create a list of hobbies
  3. Create a map of your skills with ratings (1-5)
  4. Print a formatted message using all these variables

Here’s a starter template:

void main() {
  // TODO: Create variables for name, age, and favorite programming languages
  
  // TODO: Create a list of hobbies
  
  // TODO: Create a map of skills with ratings
  
  // TODO: Print a formatted message using these variables
}

Would you like me to guide you through more complex variable concepts or shall we move on to another aspect of Dart programming?