When you’re just getting started with Flutter, one thing you’ll quickly realize is that everything runs on Dart. Dart is the core language that powers Flutter, and like any other programming language, it has its own set of basic building blocks: data types, variables, and operators.
What Are Data Types?
Data types in Dart determine what kind of data a variable can hold. They help the compiler understand how much memory to allocate and how to handle the data.
- int: Used for whole numbers. Example:
int age = 25;
- double: For decimal numbers. Example:
double height = 5.9;
- String: Text data. Example:
String name = "Alice";
- bool: Boolean values (true or false). Example:
bool isLoggedIn = true;
- List: Collection of items. Example:
List<String> fruits = ['Apple', 'Banana'];
Declaring Variables
In Dart, variables can be declared using var
, final
, or const
, depending on how you want them to behave.
- var: Automatically detects the data type.
var city = "New York";
- final: Value can’t be changed once assigned.
final country = "USA";
- const: Like
final
, but value is known at compile-time.const pi = 3.14;
You can also be explicit:
String username = "flutter_dev";
int followers = 1000;
bool isVerified = false;
Understanding Operators
Operators are symbols used to perform operations on variables and values.
Arithmetic Operators
+
Addition-
Subtraction*
Multiplication/
Division~/
Integer Division%
Modulus (remainder)
Comparison Operators
==
Equal to!=
Not equal to>
Greater than<
Less than>=
Greater than or equal to<=
Less than or equal to
Logical Operators
&&
AND||
OR!
NOT
Type Inference vs Explicit Typing
Dart supports type inference, meaning you don’t always need to specify the type of a variable. But you can if you want to improve readability or maintain strict type-checking.
var name = "John"; // Inferred as String
String surname = "Doe"; // Explicit
Mixing It All Together
Here’s a quick example that combines all these concepts:
void main() {
int x = 10;
int y = 5;
int sum = x + y;
print("Sum is $sum");
bool isGreater = x > y;
print("Is x greater than y? $isGreater");
final message = "Flutter is fun!";
print(message);
}
Understanding Dart’s basic elements like data types, variables, and operators will make your Flutter journey much smoother. These are the tools you'll use every day, so take some time to experiment and get comfortable with them!
0 Comments:
Post a Comment