0
0
Fluttermobile~10 mins

Dependency injection (GetIt) 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 singleton instance using GetIt.

Flutter
final getIt = GetIt.instance;

void setup() {
  getIt.[1]<MyService>(MyService());
}
Drag options to blanks, or click blank then click option'
AregisterLazySingleton
BregisterFactory
CregisterSingleton
DregisterInstance
Attempts:
3 left
💡 Hint
Common Mistakes
Using registerFactory instead of registerSingleton
Confusing lazy singleton with immediate singleton
2fill in blank
medium

Complete the code to retrieve the registered service instance from GetIt.

Flutter
final myService = getIt.[1]<MyService>();
Drag options to blanks, or click blank then click option'
Aget
Bfetch
Cfind
Dretrieve
Attempts:
3 left
💡 Hint
Common Mistakes
Using find or fetch which are not GetIt methods
Trying to access the instance directly without get()
3fill in blank
hard

Fix the error in this code to register a lazy singleton with GetIt.

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

Fill both blanks to register and then retrieve a service instance using GetIt.

Flutter
getIt.[1]<MyService>(MyService());
final service = getIt.[2]<MyService>();
Drag options to blanks, or click blank then click option'
AregisterSingleton
Bget
CregisterFactory
Dfind
Attempts:
3 left
💡 Hint
Common Mistakes
Using registerFactory instead of registerSingleton for single instance
Using find instead of get to retrieve instance
5fill in blank
hard

Fill all three blanks to register a lazy singleton, retrieve it, and call a method.

Flutter
getIt.[1]<MyService>(() => MyService());
final service = getIt.[2]<MyService>();
service.[3]();
Drag options to blanks, or click blank then click option'
AregisterLazySingleton
Bget
Cinitialize
DregisterSingleton
Attempts:
3 left
💡 Hint
Common Mistakes
Using registerSingleton instead of lazy singleton
Calling a method that does not exist on the service