What if you could change your app's data source without rewriting your UI code?
Why Repository pattern in Flutter? - Purpose & Use Cases
Imagine building a Flutter app that fetches user data from the internet, saves some info locally, and also reads from a database. Without a clear plan, you might mix all these data calls directly inside your UI code.
This makes your app messy and hard to fix when something breaks.
When you put data fetching code everywhere, it becomes slow to update or change. If the API changes, you must hunt through many files to fix bugs. It's easy to make mistakes and hard to test parts separately.
The Repository pattern acts like a smart middleman. It hides all the data details behind one simple interface. Your UI talks only to the repository, which decides where to get data from--API, cache, or database.
This keeps your code clean, easy to update, and test.
var user = await fetchUserFromApi(); var cachedUser = await readUserFromCache(); // UI mixes all data calls
var user = await userRepository.getUser(); // UI calls one method, no data details here
You can change where data comes from without touching your UI, making your app flexible and easier to maintain.
Think of a shopping app where product info can come from the internet or local storage. Using the Repository pattern, the app seamlessly switches sources without breaking the user experience.
Repository pattern separates data logic from UI code.
It makes apps easier to maintain and test.
It hides complex data sources behind simple methods.
Practice
Repository pattern in Flutter app development?Solution
Step 1: Understand the role of Repository pattern
The Repository pattern is designed to separate data access from the UI, so the UI does not directly handle data fetching or storage.Step 2: Identify the correct purpose
Among the options, only separating data access logic from UI code matches the Repository pattern's purpose.Final Answer:
To separate data access logic from UI code -> Option DQuick Check:
Repository pattern = separate data access from UI [OK]
- Confusing Repository with UI widget creation
- Thinking Repository manages navigation
- Assuming Repository handles user input
Solution
Step 1: Recall Dart syntax for abstract classes
In Dart, interfaces are created using abstract classes with method signatures but no bodies.Step 2: Check method signature for async fetching
The method should return a Future to indicate asynchronous fetching.Final Answer:
abstract class UserRepository { Future fetchUsers(); } -> Option AQuick Check:
Dart interface = abstract class with Future method [OK]
- Using 'interface' keyword which Dart doesn't have
- Missing Future for async methods
- Incorrect method syntax or return type
class UserRepositoryImpl implements UserRepository {
@override
Future fetchUsers() async {
return ['Alice', 'Bob'];
}
}
void main() async {
var repo = UserRepositoryImpl();
var users = await repo.fetchUsers();
print(users.length);
}Solution
Step 1: Understand the fetchUsers method
The method returns a Future with a list containing two strings: 'Alice' and 'Bob'.Step 2: Analyze the main function output
The code awaits the fetchUsers call, then prints the length of the list, which is 2.Final Answer:
2 -> Option AQuick Check:
List length = 2 [OK]
- Confusing printed list with its length
- Ignoring async/await causing errors
- Assuming fetchUsers returns empty list
class UserRepositoryImpl implements UserRepository {
Future fetchUsers() {
return ['Alice', 'Bob'];
}
}Solution
Step 1: Check return type of fetchUsers
The method signature says it returns Future, but the returned value is a List<String> without wrapping in Future.Step 2: Identify type mismatch and missing Future
Returning a plain list instead of a Future causes a type mismatch error.Final Answer:
fetchUsers must return Future, but returns List<String> -> Option BQuick Check:
Return type mismatch = fetchUsers must return Future, but returns List<String> [OK]
- Forgetting to wrap return value in Future
- Ignoring type mismatch between User and String
- Assuming async keyword is always required
Solution
Step 1: Understand Repository pattern goal
The pattern centralizes data access, hiding multiple sources behind one interface.Step 2: Evaluate options for combining data sources
Create a repository that fetches from local DB first, then updates from remote API, exposing a single method to UI combines local and remote data inside one repository method, exposing a simple interface to UI, which matches the pattern.Step 3: Reject options that expose data sources to UI
Options B and D let UI handle data sources, breaking separation. Use only remote API and ignore local DB to simplify code ignores local data, losing benefits.Final Answer:
Create a repository that fetches from local DB first, then updates from remote API, exposing a single method to UI -> Option CQuick Check:
Repository hides multiple sources behind one interface [OK]
- Letting UI handle multiple data sources
- Ignoring local cache benefits
- Splitting data sources into separate repositories without abstraction
Ready to prove it?
Pass the test and this pattern moves unknown
