0
0
Fluttermobile~20 mins

Service locator pattern in Flutter - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Service Locator Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding the purpose of the Service Locator pattern

What is the main benefit of using the Service Locator pattern in Flutter app testing?

AIt replaces the need for unit tests by providing integration tests only.
BIt automatically generates UI tests without writing code.
CIt centralizes the creation and access of services, making it easier to swap implementations during tests.
DIt enforces strict type checking at compile time for all services.
Attempts:
2 left
💡 Hint

Think about how managing dependencies helps testing.

Predict Output
intermediate
2:00remaining
Output of service locator registration and retrieval

Given the following Flutter service locator code snippet, what will be printed?

Flutter
final locator = ServiceLocator();

class ApiService {
  String fetchData() => 'Real Data';
}

void main() {
  locator.register<ApiService>(ApiService());
  final api = locator.get<ApiService>();
  print(api.fetchData());
}
AThrows an exception: Service not found
BReal Data
Cnull
DInstance of 'ApiService'
Attempts:
2 left
💡 Hint

Check what fetchData() returns and what is printed.

assertion
advanced
2:00remaining
Correct assertion for mocked service call

In a Flutter test using a service locator, which assertion correctly verifies that the mocked ApiService's fetchData method returns 'Mock Data'?

Flutter
class MockApiService extends Mock implements ApiService {}

void main() {
  final mockApi = MockApiService();
  when(() => mockApi.fetchData()).thenReturn('Mock Data');
  locator.register<ApiService>(mockApi);
  final api = locator.get<ApiService>();
  final result = api.fetchData();
  // Which assertion below is correct?
}
Aexpect(result, equals('Mock Data'));
Bexpect(result, isNull);
Cexpect(result, equals('Real Data'));
Dexpect(result, isNotEmpty);
Attempts:
2 left
💡 Hint

Check what the mock returns and what the assertion compares.

🔧 Debug
advanced
2:00remaining
Debugging service locator registration error

Why does the following Flutter test code throw an exception when trying to retrieve a service?

Flutter
final locator = ServiceLocator();

class ApiService {}

void main() {
  final api = locator.get<ApiService>();
  print(api);
}
ABecause ApiService was never registered before calling get, causing a 'Service not found' error.
BBecause locator.get requires an async call which is missing.
CBecause ApiService class is missing a default constructor.
DBecause print cannot output class instances directly.
Attempts:
2 left
💡 Hint

Check if the service was registered before retrieval.

framework
expert
2:00remaining
Best practice for resetting service locator in Flutter tests

In Flutter testing, what is the best way to ensure the service locator is clean before each test to avoid state leakage?

ADo nothing; service locator state does not affect tests.
BCreate a new service locator instance inside each test without resetting.
CRely on Flutter's hot reload to clear the locator state automatically.
DCall locator.reset() or equivalent method in the setUp() function before each test.
Attempts:
2 left
💡 Hint

Think about test isolation and shared state.