0
0
Fluttermobile~10 mins

Service locator 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 register a service using the service locator.

Flutter
final getIt = GetIt.instance;

void setup() {
  getIt.[1]<MyService>(MyServiceImpl());
}
Drag options to blanks, or click blank then click option'
AregisterFactory
BregisterSingleton
CregisterLazySingleton
DregisterInstance
Attempts:
3 left
💡 Hint
Common Mistakes
Using registerFactory instead of registerSingleton causes a new instance every time.
Using registerLazySingleton delays creation until first use, not immediate.
2fill in blank
medium

Complete the code to retrieve the registered service from the service locator.

Flutter
final myService = getIt.[1]<MyService>();
Drag options to blanks, or click blank then click option'
Aget
Bfind
Cfetch
Dlocate
Attempts:
3 left
💡 Hint
Common Mistakes
Using find or fetch causes errors because those methods do not exist.
Using locate is not a method in GetIt.
3fill in blank
hard

Fix the error in the code to register a lazy singleton service.

Flutter
getIt.[1]<MyService>(() => MyServiceImpl());
Drag options to blanks, or click blank then click option'
AregisterSingleton
BregisterFactory
CregisterLazySingleton
DregisterInstance
Attempts:
3 left
💡 Hint
Common Mistakes
Using registerSingleton creates the instance immediately, not lazily.
Using registerFactory creates a new instance every time, not singleton.
4fill in blank
hard

Fill both blanks to register and retrieve a service correctly.

Flutter
getIt.[1]<Logger>(() => LoggerImpl());
final logger = getIt.[2]<Logger>();
Drag options to blanks, or click blank then click option'
AregisterLazySingleton
Bget
CregisterSingleton
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using registerSingleton instead of lazy singleton changes when instance is created.
Using find instead of get causes runtime errors.
5fill in blank
hard

Fill all three blanks to register a factory, retrieve the service, and reset the locator.

Flutter
getIt.[1]<ApiService>(() => ApiServiceImpl());
final api = getIt.[2]<ApiService>();
await getIt.[3]();
Drag options to blanks, or click blank then click option'
AregisterFactory
Bget
Creset
DregisterSingleton
Attempts:
3 left
💡 Hint
Common Mistakes
Using registerSingleton instead of registerFactory changes instance creation behavior.
Forgetting to await reset causes unexpected behavior.