3 Variables and Data Types
The Magician’s Kit
3.1 Magic Potions and Spells: Dart Variables and Data Types
Welcome, future Dart wizards, to another enchanting chapter of our coding adventure. Today, we’ll explore the mystical world of Dart variables and data types. You’ll make variables dance to your tunes and morph data types like a true sorcerer by the end.
3.2 What’s in a name? Everything, when it’s a Variable!
First up, let’s meet our shapeshifter friend - Variables. They’re the magical containers in programming, storing, and changing their content as you command. They’re like the Room of Requirement in Harry Potter – they can become anything you need them to be. Need to remember a user’s name? Variables to the rescue. Want to calculate the score of your cheese-rolling game? Variables have your back!
3.3 The Wonderful World of Data Types
Now, imagine trying to stuff an elephant into a suitcase or pour an ocean into a teacup; impossible, right? That’s where data types come into play. There are different kinds of boxes in Dart - sorry, I mean variables - to store different kinds of data. You have:
- Numbers: Dart gives you two types of numeric containers,
int
for whole numbers (like the number of cats you own) anddouble
for fractional ones (like the exact amount of coffee you drink daily in liters).
= 5; // You have five adorable cats
int numberOfCats = 0.75; // You drink 0.75 liters of coffee a day, cheers! double litersOfCoffee
Strings: These are for text. Remember the name of your first pet or your favorite quote? Those are strings, and they are always written between ‘single quotes’ or “double quotes.”
String petName = 'Fluffy'; // Your pet's name is Fluffy, aww!
String favoriteQuote = "Coffee first, schemes later."; // Wise choice
Booleans: These are the simplest of data types, storing only true
or false
. They’re like the light switches of the coding world, deciding the path of your conditionals.
= true; // You love coding, right? bool lovesCoding
3.4 Declaring and Assigning Variables - Let’s Get the Party Started!
Now that we have our Dart-flavored containers, let’s fill them up. Declaring a variable is like sending a party invitation to a specific data type. You’re telling it to reserve a spot in the memory and wait for the party to start.
String party; // Declared a variable named 'party' of type 'String'
Assigning a variable, on the other hand, is like kicking off the party by filling that memory spot with a value.
= 'Started'; // The party has started! party
You can also do both at once - send the invitation and start the party:
String party = 'Started'; // Declared and assigned in one line
3.5 Final, Const, Var, and Late - The Special Keywords
Think of these as the VIPs of the Dart world.
final
: This keyword is used when you have a variable you don’t want to change after it’s assigned. It’s like a stubborn cat that found a comfortable spot on the couch and won’t move, no matter what.
String favoriteFood = 'Pizza'; // Pizza is love, Pizza is life final
const
: This is for values you know are constant and won’t change even before runtime, like a day has 24 hours or a minute has 60 seconds.
const int hoursInDay = 24; // Time is constant... unless you're Doctor Who
var
: Dart’s version of “Whatever!”. If you’re feeling a bit lazy and want to avoid defining the data type, Dart’s got your back. It will figure out the data type based on the value you assign.
var lazyVar = 'I am a string'; // Dart knows this is a string
late
: This is Dart’s way of saying, “Better late than never”. Use this when you want to declare a variable but can’t immediately assign it a value.
String lateToTheParty; // We'll assign this variable later late
So, that’s your introductory crash course on Dart variables and data types. Remember, practice is key to mastering these concepts. So, start brewing your magic potions and casting your spells, and remember to have fun while you’re at it!