Object-Oriented Programming (OOP) Concepts in Dart & Flutter
Teqani Blogs
Writer at Teqani
Dart, the language Flutter is built upon, is a fully object-oriented language. Flutter harnesses the power of OOP to build modular, scalable, and reusable UI components. This article delves into the core OOP concepts and how they are applied in the Flutter framework, including examples and use cases.
Class & Object
A class defines a blueprint for creating objects, while an object is an instance of that class. Classes encapsulate data (attributes) and behavior (methods) related to a specific entity.
Example:
class Product {
final String name;
final double price;
Product(this.name, this.price);
void display() => print("Product: $name - ₹$price");
}
final product = Product("Laptop", 75000);
product.display();
Use Case: Every widget, controller, or model (e.g., Text, User, Invoice) in Flutter is an object derived from a class.
Encapsulation
Encapsulation involves hiding the internal state of an object and exposing only the necessary data through getters, setters, or methods. This helps protect the integrity of the data and prevents unwanted modifications.
Example:
class Counter {
int _count = 0; // private variable
int get count => _count;
void increment() {
_count++;
}
}
Use Case: Commonly used in state management (e.g., ChangeNotifier, Controller in GetX) to protect app state and control UI updates.
Inheritance
Inheritance allows one class (subclass or derived class) to inherit properties and behaviors from another class (superclass or base class). This promotes code reuse and reduces redundancy.
Example:
class BaseScreen extends StatelessWidget {
const BaseScreen({super.key});
Widget buildBody() => const Text("Default Body");
@override
Widget build(BuildContext context) {
return Scaffold(body: Center(child: buildBody()));
}
}
class HomeScreen extends BaseScreen {
@override
Widget buildBody() => const Text("Welcome to Home");
}
Use Case: Used for shared screen layouts, base repositories, and service classes.
Polymorphism
Polymorphism means "many forms." It allows objects of different classes to be treated as objects of a common type. This is often achieved through interfaces or abstract classes.
Example:
abstract class Payment {
void pay();
}
class CardPayment extends Payment {
@override
void pay() => print("Paid using Card");
}
class UpiPayment extends Payment {
@override
void pay() => print("Paid using UPI");
}
Use Case: Used in Flutter for abstract widgets (StatelessWidget, StatefulWidget) and APIs where multiple implementations behave differently under a common interface.
Abstraction
Abstraction hides complex implementation details and exposes only the essential information to the user. This simplifies the usage of complex systems and promotes modularity.
Example:
abstract class AuthService {
Future<void> login(String user, String pass);
}
class FirebaseAuthService extends AuthService {
@override
Future<void> login(String user, String pass) async {
// Firebase login logic
}
}
Use Case: Employed in Repository pattern, services, or APIs – makes switching data sources (like Firebase → REST API) simple.
- Class & Object: Blueprint and instance - Widgets, models, services
- Encapsulation: Hide internal logic - Providers, controllers
- Inheritance: Reuse parent code - Custom screens, base widgets
- Polymorphism: Override parent behavior - Widget build methods
- Abstraction: Hide complexity - API layers, services
All blogs are certified by our company and reviewed by our specialists
Issue Number: #a2dc3b0a-22ea-4b8d-ae9c-776b4f228594