Dart Basics for Flutter Developers
Dart Basics for Flutter Developers
Dart is the programming language used to build Flutter apps. Understanding Dart fundamentals helps you write better Flutter code.
1. Variables and Data Types
dart
Copy
Edit
// Variable declaration
int age = 25;
double price = 19.99;
String name = 'John';
bool isLoggedIn = true;
var city = 'New York'; // type inferred
2. Functions
dart
Copy
Edit
// Simple function
int add(int a, int b) {
return a + b;
}
// Arrow function (short syntax)
int multiply(int a, int b) => a * b;
3. Control Flow
dart
Copy
Edit
// If-else
if (age > 18) {
print('Adult');
} else {
print('Minor');
}
// For loop
for (int i = 0; i < 5; i++) {
print(i);
}
// While loop
int count = 0;
while (count < 3) {
print(count);
count++;
}
4. Collections
dart
Copy
Edit
// List
List<String> fruits = ['Apple', 'Banana', 'Orange'];
// Map (dictionary)
Map<String, int> scores = {'Alice': 90, 'Bob': 85};
// Iterate list
for (var fruit in fruits) {
print(fruit);
}
5. Classes and Objects
dart
Copy
Edit
class Person {
String name;
int age;
Person(this.name, this.age);
void greet() {
print('Hello, my name is $name');
}
}
void main() {
var p = Person('Alice', 30);
p.greet(); // Output: Hello, my name is Alice
}
6. Null Safety
Dart is null-safe, meaning variables can’t be null unless explicitly declared nullable.
dart
Copy
Edit
int? nullableAge; // Can be null
int nonNullableAge = 18; // Cannot be null
7. Asynchronous Programming
dart
Copy
Edit
Future<String> fetchData() async {
await Future.delayed(Duration(seconds: 2));
return 'Data loaded';
}
void main() async {
print('Fetching...');
String data = await fetchData();
print(data);
}
Summary
Concept Example
Variables int age = 25;
Functions int add(int a, int b) => a + b;
Control Flow if-else, for, while loops
Collections List, Map
Classes class Person { ... }
Null Safety int? nullableAge;
Async/Await Future, async/await
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
What is Flutter? An Introduction for Beginners
Comments
Post a Comment