Embark on a journey of knowledge! Take the quiz and earn valuable credits.
Take A QuizChallenge yourself and boost your learning! Start the quiz now to earn credits.
Take A QuizUnlock your potential! Begin the quiz, answer questions, and accumulate credits along the way.
Take A Quiz
🧭 What You’ll Learn
In this chapter, you’ll explore the Dart programming
language, which powers Flutter development. You’ll learn:
💡 Why Dart?
Flutter apps are written in Dart because:
🧱 Dart Basics
✅ Hello World in Dart
dart
void
main() {
print('Hello, Dart!');
}
You can run this in a Dart file or inside DartPad.
📌 Variables and Types
Dart is optionally typed, meaning you can use var, final, or
explicitly specify types.
dart
var
name = 'Flutter';
int
age = 2;
double
price = 19.99;
bool
isAvailable = true;
Constant vs Final
dart
final
currentYear = 2024; // Set once at runtime
const
pi = 3.14159; // Compile-time
constant
🧠 Type Table
Type |
Example |
int |
int age = 25; |
double |
double pi =
3.14; |
String |
String name = 'Dart'; |
bool |
bool isDone =
false; |
var |
var value = 'hello'; |
🔁 Control Flow
✅ If-Else
dart
int
score = 85;
if
(score >= 90) {
print('A');
}
else if (score >= 80) {
print('B');
}
else {
print('C');
}
✅ Loops
dart
for
(var i = 0; i < 5; i++) {
print('i = $i');
}
while
(score > 0) {
score -= 10;
print('Score: $score');
}
🧩 Functions in Dart
dart
int
add(int a, int b) {
return a + b;
}
//
Arrow syntax
String
greet(String name) => 'Hello, $name';
void
main() {
print(add(3, 4)); // 7
print(greet('Flutter')); // Hello, Flutter
}
🔷 Null Safety
Introduced in Dart 2.12, null safety ensures variables can’t
be null unless specified.
dart
String?
name = null; // Nullable
String
username = 'guest'; // Non-nullable
//
Null-aware operator
print(name
?? 'default'); // prints 'default'
✅ Null-Aware Operators
Operator |
Meaning |
?? |
Default value if null |
?. |
Safe
navigation (call method if not null) |
! |
Assert not null |
🧱 Object-Oriented Dart
✅ Classes and Constructors
dart
class
User {
String name;
int age;
// Constructor
User(this.name, this.age);
void greet() {
print('Hi, I\'m $name and I\'m $age');
}
}
void
main() {
var user = User('Alex', 30);
user.greet(); // Hi, I'm Alex and I'm 30
}
🔁 Named and Optional
Parameters
dart
class
Product {
String name;
double price;
Product({required this.name, this.price =
0.0});
}
var
p = Product(name: 'Phone');
print(p.price);
// 0.0
🧠 Dart Object Syntax
Summary
Concept |
Syntax Example |
Constructor |
ClassName(this.property) |
Named Params |
{required
String name} |
Method |
void greet() =>
print('Hi'); |
Getters/Setters |
get name
=> _name; set name(n) => _name = n; |
🔢 Collections in Dart
✅ Lists
dart
var
colors = ['red', 'green', 'blue'];
colors.add('yellow');
print(colors.length);
// 4
✅ Sets (no duplicates)
dart
var
uniqueNumbers = {1, 2, 3};
uniqueNumbers.add(2);
// ignored
✅ Maps (key-value pairs)
dart
var
user = {
'name': 'John',
'age': 28
};
print(user['name']);
// John
🧠 List Operations Table
Method |
Description |
.add() |
Add single item |
.addAll() |
Add multiple
items |
.remove() |
Remove by value |
.contains() |
Check if value
exists |
.forEach() |
Loop through list |
🔄 Async Programming with
Futures
✅ Future Basics
dart
Future<String>
fetchData() async {
await Future.delayed(Duration(seconds: 2));
return 'Data loaded!';
}
void
main() async {
print(await fetchData());
}
✅ Error Handling
dart
try
{
var data = await fetchData();
print(data);
}
catch (e) {
print('Error: $e');
}
🔄 Stream Basics
Use streams when you expect multiple asynchronous values
over time (e.g., live data or countdowns).
dart
Stream<int>
counter() async* {
for (int i = 0; i < 5; i++) {
await Future.delayed(Duration(seconds: 1));
yield i;
}
}
🧠 Dart vs JavaScript
Syntax Comparison
Feature |
Dart |
JavaScript |
Variable |
var name = 'Dart' |
let name = 'JS' |
Function |
int sum(a, b)
=> a + b; |
const sum =
(a,b) => a + b; |
Class |
class User {} |
class User {} |
Async/await |
await
fetchData() |
await
fetchData() |
✅ Summary
You’ve now learned the fundamentals of Dart, including:
With Dart as your tool, you’re ready to start building
beautiful UIs in Flutter!
Answer:
Flutter is an open-source UI toolkit by Google that enables you to build
natively compiled apps for mobile, web, and desktop from a single codebase.
It’s fast, efficient, and perfect for building beautiful, responsive UIs with a
single programming language—Dart.
Answer:
Not necessarily. Dart is easy to learn, especially if you’re familiar with
JavaScript, Java, or C#. Most Flutter tutorials introduce Dart basics along the
way, making it beginner-friendly.
Answer:
Yes! Flutter allows you to develop apps for Android and iOS from a single
codebase. You can also compile apps for web, Windows, macOS, and Linux using
the same project.
Answer:
You’ll need a modern operating system like Windows 10+, macOS, or Linux, along
with tools like Git, an IDE (VS Code or Android Studio), and device emulators
or simulators for testing.
Answer:
Hot reload is a Flutter feature that allows you to instantly see code changes
in your running app without restarting the entire application. It boosts
productivity and speeds up the development process.
Answer:
You can download the Flutter SDK from flutter.dev, extract it to a folder, add it to your system
PATH, and run flutter doctor to check for setup issues. IDE plugins are also
available for Android Studio and VS Code.
Answer:
Yes, Flutter supports web development, though it’s still evolving compared to
mobile. It’s ideal for building highly interactive web apps or PWAs with
consistent UI and shared code.
Answer:
Widgets are the building blocks of a Flutter app. Everything you see on
screen—text, buttons, layout elements, animations—is a widget that can be
customized or nested inside other widgets.
Answer:
Both are popular cross-platform frameworks, but Flutter offers better
performance due to its native rendering engine and avoids using platform views.
React Native uses JavaScript and relies more on native components.
Answer:
Start with the official Flutter documentation, Flutter YouTube channel,
freeCodeCamp, and
community blogs on Medium or Dev.to. You can also explore interactive learning
on Flutter Apprentice.
Please log in to access this content. You will be redirected to the login page shortly.
LoginReady to take your education and career to the next level? Register today and join our growing community of learners and professionals.
Comments(0)