Dart Basics for Flutter Developers
π¦ Dart Basics for Flutter Developers
Dart is the programming language used to build Flutter apps. If you’re a Flutter developer, understanding Dart is essential for writing clean, efficient, and scalable apps.
This guide covers the core Dart concepts every Flutter developer should know.
π¦ 1. Variables and Data Types
Dart is statically typed, but it also supports type inference.
dart
Copy
Edit
var name = 'Alice'; // Dart infers this as a String
int age = 30;
double height = 5.9;
bool isLoggedIn = true;
String city = 'London';
You can also use dynamic if the type may change:
dart
Copy
Edit
dynamic something = 'Hello';
something = 42; // Still valid
π 2. Control Flow (if, else, loops)
dart
Copy
Edit
if (age > 18) {
print('Adult');
} else {
print('Minor');
}
for (var i = 0; i < 5; i++) {
print(i);
}
while (isLoggedIn) {
// do something
isLoggedIn = false;
}
π§° 3. Functions
Functions are first-class citizens in Dart, and you can define them with or without return types.
dart
Copy
Edit
void greet(String name) {
print('Hello, $name!');
}
int add(int a, int b) {
return a + b;
}
Arrow syntax for one-liners:
dart
Copy
Edit
int multiply(int x, int y) => x * y;
Optional and named parameters:
dart
Copy
Edit
void greetUser(String name, {String title = 'Mr./Ms.'}) {
print('Hello, $title $name');
}
π§± 4. Classes and Objects
Dart is an object-oriented language.
dart
Copy
Edit
class Person {
String name;
int age;
Person(this.name, this.age);
void sayHello() {
print('Hi, I’m $name and I’m $age years old.');
}
}
void main() {
var p = Person('Alice', 30);
p.sayHello();
}
π¦ 5. Collections (Lists, Maps, Sets)
dart
Copy
Edit
// List
List<String> fruits = ['Apple', 'Banana', 'Mango'];
// Map
Map<String, int> scores = {
'Alice': 90,
'Bob': 85,
};
// Set
Set<String> cities = {'London', 'Paris', 'New York'};
Spread operator and conditionals inside collections:
dart
Copy
Edit
var list = [1, 2, 3];
var more = [...list, if (true) 4]; // [1, 2, 3, 4]
⚙️ 6. Null Safety
Dart has null safety, which helps avoid null errors.
dart
Copy
Edit
String? name; // Can be null
String greeting = name ?? 'Guest'; // Use default if null
Late initialization:
dart
Copy
Edit
late String username; // Initialized later but not null
π§΅ 7. Asynchronous Programming
Flutter apps often use async code to handle I/O.
dart
Copy
Edit
Future<void> fetchData() async {
await Future.delayed(Duration(seconds: 2));
print('Data fetched');
}
Using .then():
dart
Copy
Edit
fetchData().then((_) => print('Done'));
✅ Conclusion
Mastering Dart is key to becoming a proficient Flutter developer. Its syntax is clean and modern, and it supports both object-oriented and functional programming styles. Once you're comfortable with Dart basics, building rich, responsive UIs with Flutter becomes much easier.
Learn Flutter Training in Hyderabad
Read More
Exploring the Flutter Directory Structure
Flutter vs React Native: Which Should You Choose?
Building Your First Flutter App Step-by-Step
Understanding Flutter Widgets: The Basics
Visit Our IHUB Talent Training Institute in Hyderabad
Comments
Post a Comment