Dart Type Conversions and Static Variables - Notes from My First Day
My Dart Learning Journey Has Begun
Hello! Today I seriously started working with Flutter. But instead of jumping straight into Flutter, I wanted to start from the basics. Because you know, the stronger the foundation a building sits on, the longer it lasts. With the same logic, I didn't want to jump into Flutter without learning Dart well.
Live Demo: Dart Type Conversions
Try type conversions and static concepts interactively:
Type Conversions
One of the first topics I encountered was type conversions. For example, you have a number in String format and you need to use it in mathematical operations. This is where parse methods come in.
String text = "3";
int number = int.parse(text);Looks simple, right? But there's an important point to note: If there's something other than a number in the String (like "3a"), the program will give an error. That's why you need to work safely with try-catch blocks when getting data from users.
A Situation I Encountered in Practice
I encountered this situation when getting age information from a user. Everything coming from TextField is a String, but I needed to do mathematical operations with age:
String userAge = "25";
int age = int.parse(userAge);
int futureAge = age + 5;
print("You will be $futureAge years old in 5 years");Static Variables - I Was a Bit Confused But I Got It
When I first read about the static topic, I didn't fully understand what it was for. But after doing a few examples, I grasped the logic.
Static variables are variables we can use directly without creating an object from the class. Think of it like this: There's a factory and this factory has a counter. Every time production is done, this counter increases. This counter needs to have a single value for the entire factory, not separate for each product.
class A {
static late int value;
static final int val = 3;
}
void main() {
// Direct use without creating object
int newValue = A.val;
print("Constant value: $newValue");
// We can change static variable
A.value = 5;
int newValue2 = A.value;
print("New value: $newValue2");
}Difference Between Final and Static
I had to learn this too: When you say final, you can't change that variable after assigning it once. So it works like a constant. In the example above, if I had tried to change the val variable, I would have gotten an error.
Why Do We Use Static?
From what I researched, static variables are very useful in these situations:
- Counters: Like how many objects were created
- Constant values: Like Pi number, tax rate
- Helper functions: Like mathematical operations
For example, if you're making a calculator class:
class Calculation {
static double add(double a, double b) {
return a + b;
}
static double subtract(double a, double b) {
return a - b;
}
}
void main() {
double result = Calculation.add(5, 3);
print("Result: $result"); // 8.0
}No need to create an object, you're using it directly by saying Calculation.add().
What I Learned Today
My first day learning Dart was quite productive. Type conversions and static concepts became clear with practice, even though they seemed complex at first. Tomorrow I'll look at enum structures and composition topics.
This is how my Flutter journey started. I hope these notes are useful for you too. Let's meet in the comments if you have questions!
Resources
- Dart Official Documentation
- My own experiences and code practices
Contact me: