When you create a fluttering application, it is easy to get stuck in the writing code that only works. But as your app increases in size and complexity, it is difficult to maintain, check and increase poorly. There are solid principles in this place.
The solid is the acronym for five design principles that help developers write clean, expanding and maintaining codes.
s – Single Responsibility Principle (SRP)
O – Open/closed Rule (OCP)
l – Leskov Alternative Rule (LSP)
I – the principle of interface separation (ISP)
Di – Dependent Ulta Rule (Dip)
In this guide, we will break every principle, explain the meaning of it, and show the examples of the practical flutter/dart code that you can apply in your plans.
Table of contents:
Provisions
Before diving, you should be:
Learn how to use the dart and the flurry.
The basic understanding of OOP concepts (classes, heritage, interface, and polymorphism).
Fluttering on your system (The installation guide of the flurry,
Familiar with running apps that flushes on an emulator or physical device.
How to enforce the principle of single responsibility (SRP) in the clash
Appreciation: There should be only one reason to change a class. This principle prevents “God’s classes” who try to do everything. Instead, each class should take a special responsibility.
Examples of fluttering
class Logger {
void log(String message) {
print(message);
}
}
class UserManager {
final Logger _logger;
UserManager(this._logger);
void addUser(String username) {
_logger.log('User $username added.');
}
}
The code specified
class Logger { ... }Class This class is responsible for logging only. It has the same waylog.class UserManager { ... }Class This class manages users (for example, adding them).final Logger _logger;Ily instead of logging directly,UserManagerDepends onLoggerClassaddUser(String username)USER is focused on user management, not logging.
By dividing responsibilities, we can change Logger With another process (such as login in a file) without touching UserManager.
SRP in Real Flicky Plans:
AuthServiceFor the logic of verificationApiServiceFor network callsDatabaseServiceFor local perseverance

How to enforce the open -off principle (OCP) in the flutter
Appreciation: Classes should be open for expansion but should be closed for amendment. This means that when adding new features, you should not need to change the existing code – just extend it.
Examples of fluttering
abstract class Shape {
double area();
}
class Circle implements Shape {
final double radius;
Circle(this.radius);
@override
double area() => 3.14 * radius * radius;
}
class Square implements Shape {
final double side;
Square(this.side);
@override
double area() => side * side;
}
The code specified
abstract class Shapeexplains the Contract contractarea()For all forms.class Circle implements ShapeCODE extends behavior without editing the existing code.class Square implements ShapeSAME adds another form.
If you want to add TriangleYou just create a new class instead of editing ShapeFor, for, for,. CircleOr Square.
OCPs in Real Flicky Plans:

How to enforce the Leskov Alternative Rule (LSP) in the flutter
Appreciation: Sub -classes should be substituted for their base classes without breaking the functionality. If your function accepts the base type, it should also accept its sub -types without any problems.
Examples of fluttering
void printArea(Shape shape) {
print('Area: ${shape.area()}');
}
void main() {
Shape circle = Circle(5);
Shape square = Square(4);
printArea(circle);
printArea(square);
}
The code specified
void printArea(Shape shape)Any works with the implementation of any classShape.circleAndsquare→ are the correct alternatives for bothShape.
LSPs in real fluttering projects:
A
TextFieldcan be replaced with APasswordFieldThe widget, as both behave like input fields.A
FirebaseAuthServicecan be replaced with AMockAuthServiceIn the test

How to enforce the Rule of Interface separation (ISP) in the clash
Appreciation: Clients should not rely on the methods they do not use. Instead of a large interface, divide it into a small, concentrated interface.
Examples of fluttering
abstract class Flyable {
void fly();
}
abstract class Swimmable {
void swim();
}
class Bird implements Flyable {
@override
void fly() => print('Bird is flying.');
}
class Fish implements Swimmable {
@override
void swim() => print('Fish is swimming.');
}
The code specified
FlyableAndSwimmableseparate agreements for flying flying and swimming.Bird implements Flyable→ Birds don’t need aswimMethodFish implements Swimmable→ Fish is not required AflyMethod
ISP in real blowing projects:
Division
AuthServiceSuch as in a small interfaceLoginServiceFor, for, for,.RegistrationServiceFor, for, for,.PasswordResetService.The widgets only implement the properties they actually need.

How to enforce the Dependent Ulta Rule (DIP) in the flutter
Appreciation: High level modules should depend on the summary, not on solid implementation. It makes your code more flexible and worthy of examination.
Examples of fluttering
abstract class Database {
void saveData(String data);
}
class SqlDatabase implements Database {
@override
void saveData(String data) {
print('SQL: Data saved -> $data');
}
}
class DataService {
final Database _database;
DataService(this._database);
void processData(String data) {
_database.saveData(data);
}
}
void main() {
Database db = SqlDatabase();
DataService service = DataService(db);
service.processData('User info');
}
The code specified
abstract class DatabaseDATA explains the data saving agreement.class SqlDatabase implements DatabaseA possible implementation.class DataService→ only depends on thisDatabaseSummary, notSqlDatabase.Database db = SqlDatabase();→ Implementation can be easily replaced (for example, withFirebaseDatabase,
Dink into real clutter plans:

Testing and reflecting with solid
Unit test Get easier because you can make fun of dependence.
Reflecting Is smooth because the responsibilities are well separated.
Code reviews Hurry up solid violations.
The final views
Following the solid principles in the flurry and dart:
Your code remains more intact.
Easy to add new features.
Testing is very easy.
These principles are not just theory, they directly improve the real -world plans. Start smaller, apply a principle at a time, and you will see your code base quickly expanding it to some extent and in the future proof.