Dart Basics & Flutter Setup
Flutter: Dart Basics & Setup Flutter is Google's cross-platform UI toolkit for iOS, Android, Web, Desktop from a single Dart codebase. Dart is a strongly-typed,…
Flutter: Dart Basics & Setup
Flutter is Google's cross-platform UI toolkit for iOS, Android, Web, Desktop from a single Dart codebase. Dart is a strongly-typed, null-safe language with async/await. Flutter renders its own widgets via Skia/Impeller — no platform UI components.
Dart Language Essentials
// Variables & types
var name = 'Alice'; // type inferred
String greeting = 'Hello';
int count = 42;
double pi = 3.14;
bool isActive = true;
// Null safety — all types non-nullable by default
String? nullable = null; // ? makes it nullable
String nonNull = nullable!; // ! force unwrap (throws if null)
String safe = nullable ?? 'default'; // null coalescing
int? length = nullable?.length; // null-aware access
// final and const
final name2 = 'Alice'; // runtime constant (set once)
const pi2 = 3.14159; // compile-time constant
// Collections
var list = [1, 2, 3];
var map = {'name': 'Alice', 'age': 30};
var set = {1, 2, 3};
// String interpolation
var msg = 'Hello, $name! You are ${30 + 1} years old.';
// Functions
int add(int a, int b) => a + b; // arrow function
void greet({required String name}) { } // named parameter
// Async/await
Future<String> fetchData() async {
await Future.delayed(Duration(seconds: 1));
return 'data';
}
// Pattern matching (Dart 3)
var result = switch (status) {
'active' => 'Active',
'inactive' => 'Inactive',
_ => 'Unknown',
};Classes & Mixins
// Class
class Animal {
final String name;
int _age; // private (convention: _ prefix)
Animal(this.name, this._age); // shorthand constructor
int get age => _age; // getter
set age(int value) { // setter
if (value > 0) _age = value;
}
String describe() => '$name, age $_age';
@override
String toString() => describe();
}
// Named constructor
class Color {
final int r, g, b;
const Color(this.r, this.g, this.b);
const Color.red() : this(255, 0, 0); // named constructor
const Color.fromHex(String hex) :
r = int.parse(hex.substring(1, 3), radix: 16),
g = int.parse(hex.substring(3, 5), radix: 16),
b = int.parse(hex.substring(5, 7), radix: 16);
}
// Mixin
mixin Flyable {
void fly() => print('Flying!');
}
class Bird extends Animal with Flyable {
Bird(super.name, super.age);
}
// Record (Dart 3)
var point = (x: 3.0, y: 4.0);
print(point.x); // 3.0Flutter Setup & Project Structure
# Install Flutter
# https://flutter.dev/docs/get-started/install
flutter --version
flutter doctor # check environment setup
flutter create my_app # create new project
cd my_app
flutter run # run on connected device/emulator
flutter run -d chrome # run as web app
flutter build apk # build Android APK
flutter build ipa # build iOS (macOS required)
flutter build web # build web app
flutter test # run tests
flutter pub get # install dependencies
flutter pub add http # add packageProject structure:
lib/
main.dart — entry point (runApp)
app.dart — MaterialApp / CupertinoApp
features/ — feature-based organization
auth/
screens/
widgets/
providers/
core/
theme/
services/
models/
test/ — widget + unit tests
assets/ — images, fonts, data files
pubspec.yaml — dependencies and assets config