SOLID is easy to memorize and easy to misuse. The goal is not to satisfy five acronyms; it is to make code that is cheap to change. Here is each principle with a Flutter-flavored example and the smell that tells you it is being violated.
S: Single Responsibility
A class should have one reason to change.
// Smell: fetches, parses, caches and formats in one place
class ProfileScreenHelper {
Future<String> loadAndFormatName() async { /* HTTP + JSON + string formatting */ }
}
Split it by reason to change: an API client (changes when the backend changes), a mapper (changes when the model changes), a presenter or BLoC (changes when the UI rules change). Each file now has exactly one reason to be edited.
O: Open/Closed
Open for extension, closed for modification. If adding a payment method means editing a growing switch, you are paying for it repeatedly.
abstract interface class PaymentMethod {
Future<PaymentResult> pay(Money amount);
}
class CardPayment implements PaymentMethod { /* ... */ }
class WalletPayment implements PaymentMethod { /* ... */ }
class Checkout {
Checkout(this._method);
final PaymentMethod _method;
Future<PaymentResult> submit(Money total) => _method.pay(total);
}
Adding Apple Pay is a new class. Checkout does not change. (Dart 3 sealed classes are a good fit when you want the compiler to force you to handle every case instead.)
L: Liskov Substitution
Subtypes must be usable wherever the base type is expected, without surprises.
abstract interface class TokenStore {
Future<String?> read();
Future<void> write(String token);
}
// Violation: a "read-only" store that throws on write
class ReadOnlyTokenStore implements TokenStore {
@override
Future<void> write(String token) => throw UnsupportedError('read only');
@override
Future<String?> read() async => _token;
}
If a caller must know which implementation it has to avoid a crash, the abstraction is wrong. Split the interface instead.
I: Interface Segregation
Do not force a class to implement methods it does not need.
// Too wide
abstract interface class UserRepository {
Future<User> get(String id);
Future<void> save(User user);
Future<void> delete(String id);
Future<List<User>> search(String query);
}
// Narrower, depended on by what each feature actually uses
abstract interface class UserReader { Future<User> get(String id); }
abstract interface class UserWriter { Future<void> save(User user); }
A profile screen that only reads depends on UserReader. A test double for it needs one method, not four.
D: Dependency Inversion
High-level code depends on abstractions, not concrete details.
class OrdersBloc {
OrdersBloc(this._repository); // abstraction, injected
final OrderRepository _repository;
}
// not: final _api = OrderApi(Dio()); // concrete detail, hard-wired
This is what makes it possible to swap a real API for a fake in tests. Wiring the concrete classes together happens in one place; see dependency injection with get_it.
When not to apply SOLID
- A small widget that is used once does not need an interface.
- Do not create an abstraction until there are two implementations or a real testing need.
- Premature abstraction is its own kind of technical debt: indirection that explains nothing.
A good heuristic: apply a principle when you feel the pain it prevents, and not before. How these ideas fit together in a full app structure is covered in Clean Architecture in Flutter.
Quick smell test
| Smell | Probably violates |
|---|---|
| "And" in a class name or description | Single Responsibility |
Growing if/else or switch on a type | Open/Closed |
UnsupportedError in an override | Liskov Substitution |
| Mocks with many unused stubbed methods | Interface Segregation |
new on a concrete service inside business logic | Dependency Inversion |
Key takeaways
- SOLID exists to lower the cost of change, not to add files.
- Dependency inversion plus small interfaces gives you most of the benefit in Flutter.
- Abstract when you have a reason; delete abstractions that explain nothing.
I apply these every day on mobile projects. See my work or say hello.