Day 3: Dart Crash Course for Flutter Developers (Part 1: Variables, Data Types, Operators)

Welcome to Day 3 of our Flutter journey! Yesterday, we set up our development environment, and you even ran your first Flutter app. Awesome! But wait, what language are we actually writing those apps in? The answer is Dart.

Dart is the programming language behind Flutter, also developed by Google. It’s designed to be a “client-optimized language for fast apps on any platform.” Don’t worry if you’re new to programming or Dart; it’s quite easy to learn, especially if you’ve seen languages like JavaScript or Java before.

Today, we’ll cover the absolute fundamentals: how Dart stores information (variables), what kind of information it can store (data types), and how it performs calculations (operators).

Think of Variables as “Labeled Boxes”

Imagine you have a bunch of empty boxes, and you want to store different things in them. To easily find what you’re looking for, you’d put a label on each box, right?

In programming, these “labeled boxes” are called variables. They are simply names you give to memory locations to store data.

To create a variable in Dart, you usually use the keyword var or specify the exact type (which we’ll talk about next).

Example:

var myName = 'Alice'; // A box labeled 'myName' holding the text 'Alice'
var age = 30;         // A box labeled 'age' holding the number 30

Once you put something in a variable, you can change it later:

var score = 100;
print(score); // Output: 100

score = 150; // Change the value in the 'score' box
print(score); // Output: 150

Data Types: What Kind of “Stuff” Can Your Boxes Hold?

Just like you wouldn’t put soup in a shoebox, programming variables need to know what kind of data they’re holding. These categories are called data types. Dart is a “strongly typed” language, meaning it cares about data types, but it’s also smart enough to often figure them out for you.

Here are the most common data types you’ll encounter in Dart:

  1. Numbers (int and double)
    • int (Integers): Whole numbers (without a decimal point).int numberOfApples = 5; int year = 2024;
    • double (Floating-point numbers): Numbers with decimal points.double price = 19.99; double pi = 3.14159;
    • Quick Tip: If you use var with a number, Dart automatically figures out if it’s an int or double.
  2. Text (String)
    • Used for any kind of text, words, sentences, or even single characters. You create strings by putting text inside single (' ') or double (" ") quotes.String greeting = 'Hello, Flutter!'; String productName = "Dart Basics Course"; String sentence = 'He said, "It\'s great!"'; // Use \ to escape internal quotes
    • You can also combine strings (called “concatenation”) using the + sign or by simply placing them next to each other:String firstName = 'John'; String lastName = 'Doe'; String fullName = firstName + ' ' + lastName; // 'John Doe' print('My name is $firstName $lastName'); // 'My name is John Doe' - This is called string interpolation!
  3. True/False (bool)
    • Short for “Boolean.” These variables can only hold one of two values: true or false. They are super important for making decisions in your code.bool isLoggedIn = true; bool hasError = false;
  4. Lists (like Arrays in other languages)
    • Used to store a collection of items, ordered by index (starting from 0).List<String> fruits = ['apple', 'banana', 'cherry']; List<int> numbers = [10, 20, 30, 40]; print(fruits[0]); // Output: 'apple'
  5. Maps (like Dictionaries/Objects in other languages)
    • Used to store collections of key-value pairs. Think of it like a real-world dictionary where each word (key) has a definition (value).Map<String, String> user = { 'name': 'Alice', 'email': 'alice@example.com', 'city': 'New York' }; print(user['name']); // Output: 'Alice'

Operators: Doing Stuff with Your Variables

Operators are special symbols that perform operations on values and variables. You use them all the time in everyday math, and they work similarly in Dart.

  1. Arithmetic Operators (for Math!):
    • + (Addition)
    • - (Subtraction)
    • * (Multiplication)
    • / (Division – results in a double)
    • ~/ (Integer Division – results in an int, discards remainder)
    • % (Modulo – gives the remainder of a division)
    int a = 10; int b = 3; print(a + b); // 13 print(a - b); // 7 print(a * b); // 30 print(a / b); // 3.3333... (double) print(a ~/ b); // 3 (int) print(a % b); // 1 (remainder of 10 / 3)
  2. Assignment Operators (for Changing Values):
    • = (Assigns a value)
    • +=, -=, *=, /=, etc. (Shorthand for operations and assignment)
    int x = 5; x += 3; // Same as x = x + 3; (x is now 8) print(x); // 8
  3. Comparison (Relational) Operators (for Comparing Values – results in bool):
    • == (Is equal to)
    • != (Is not equal to)
    • > (Greater than)
    • < (Less than)
    • >= (Greater than or equal to)
    • <= (Less than or equal to)
    int num1 = 10; int num2 = 20; print(num1 == num2); // false print(num1 < num2); // true
  4. Logical Operators (for Combining True/False):
    • && (AND: true if both are true)
    • || (OR: true if at least one is true)
    • ! (NOT: reverses true to false, or false to true)
    bool isRaining = true; bool isSunny = false; print(isRaining && isSunny); // false print(isRaining || isSunny); // true print(!isRaining); // false

Putting it Together: A Simple Example

void main() {
    // Variables and Data Types
    String appName = 'My Awesome Flutter App';
    int versionNumber = 1;
    double releasePrice = 0.99;
    bool isFree = false;
    List<String> features = ['Login', 'Dashboard', 'Settings'];
    Map<String, String> developerInfo = {
        'name': 'Dev Team',
        'contact': 'dev@example.com'
    };

    print('App Name: $appName');
    print('Version: $versionNumber');

    // Operators
    int downloads = 500;
    downloads += 150; // New downloads!
    print('Total Downloads: $downloads');

    if (downloads > 600 && isFree == false) {
        print('We hit our download target! Time for a celebration!');
    } else {
        print('Keep promoting the app!');
    }

    print('First feature: ${features[0]}');
    print('Developer contact: ${developerInfo['contact']}');
}

If you run this Dart code, you’ll see how these basic building blocks work together to store and manipulate information.

Interactive Dart Playground

Dart Fundamentals

LIVE EDITOR

                 

Conclusion

You’ve just had your first taste of Dart’s fundamental concepts: variables, data types, and operators. These are the basic ingredients you’ll use to store data, make calculations, and create the logic for your Flutter applications.

Tomorrow, we’ll continue our Dart crash course by diving into functions, classes, and a crucial concept for Flutter: asynchronous programming (async, await). Get ready to build more complex and powerful logic!

Share