0
0
Fluttermobile~10 mins

Repository pattern in Flutter - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a repository interface in Flutter.

Flutter
abstract class UserRepository {
  Future<List<User>> [1]();
}
Drag options to blanks, or click blank then click option'
AgetUsers
BfetchUsers
CloadUsers
DretrieveUsers
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that are too vague or uncommon.
Not using Future for asynchronous data fetching.
2fill in blank
medium

Complete the code to implement the repository interface with a remote data source.

Flutter
class RemoteUserRepository implements UserRepository {
  final ApiClient apiClient;

  RemoteUserRepository(this.apiClient);

  @override
  Future<List<User>> [1]() {
    return apiClient.fetchUsers();
  }
}
Drag options to blanks, or click blank then click option'
AgetUsers
BretrieveUsers
CloadUsers
DfetchUsers
Attempts:
3 left
💡 Hint
Common Mistakes
Using a different method name than the interface.
Not overriding the method properly.
3fill in blank
hard

Fix the error in the repository method to handle exceptions properly.

Flutter
Future<List<User>> getUsers() async {
  try {
    final users = await apiClient.fetchUsers();
    return users;
  } catch ([1]) {
    throw Exception('Failed to load users');
  }
}
Drag options to blanks, or click blank then click option'
Aerror
Bexception
Ce
Derr
Attempts:
3 left
💡 Hint
Common Mistakes
Using invalid variable names.
Not catching the exception variable at all.
4fill in blank
hard

Fill both blanks to create a repository that caches data locally after fetching remotely.

Flutter
class CachedUserRepository implements UserRepository {
  final UserRepository remoteRepo;
  final UserCache cache;

  CachedUserRepository(this.remoteRepo, this.cache);

  @override
  Future<List<User>> getUsers() async {
    final cachedUsers = cache.[1]();
    if (cachedUsers != null) {
      return cachedUsers;
    }
    final users = await remoteRepo.[2]();
    cache.save(users);
    return users;
  }
}
Drag options to blanks, or click blank then click option'
AgetCachedUsers
BfetchUsers
CgetUsers
DloadUsers
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing method names between cache and remote repository.
Not checking for null before returning cached data.
5fill in blank
hard

Fill all three blanks to define a repository pattern with interface, implementation, and error handling.

Flutter
abstract class [1] {
  Future<List<User>> getUsers();
}

class [2] implements [1] {
  final ApiClient apiClient;

  [2](this.apiClient);

  @override
  Future<List<User>> getUsers() async {
    try {
      return await apiClient.fetchUsers();
    } catch (e) {
      throw Exception('Error fetching users');
    }
  }
}
Drag options to blanks, or click blank then click option'
AUserRepository
BRemoteUserRepository
CUserService
DUserApi
Attempts:
3 left
💡 Hint
Common Mistakes
Using inconsistent class names.
Not implementing the interface correctly.