Introduction To GetIt Library in Flutter
--
GetIt Library is a flutter library that’s usually used as a singleton for your codebase. It’s great as a service locator — as Idan mentioned—but can also be used for other things, such as adding a global state, lazily injecting a module, creating scopes in singleton, etc., etc. Mainly, people use it as a singleton to simply store global objects.
There are several use cases for GetIt.
GetIt Use Cases
- Storing data repository classes (repository as in data providers such as an API connector and local database connector)
- Providing a way to communicate between state classes (e.g. providers, bloc, classes)
- Modularize singleton with names (more on this later).
GetIt Non-Use Cases
Usually, everything that’s not listed above because singleton can be dangerous if not used correctly because it introduces tightly coupled classes. For example, here’s some example classes that should never be in GetIt:
- Logic Classes that perform calculations.
- Helper classes
- Contexts
- A dummy object that’s used as a mock data
- ANY widget
Installing GetIt
Add get_it to your dependency list in pubspec.yaml
(replace latest
to current version)
dependencies:
get_it: latest
Using GetIt
One of the way to use get_it
is to create a file like get_it.dart
and exporting both thesetup()
function and the getIt
variable.
// get_it.dart
final getIt = GetIt.instance;
// make setup async if you want to initiate db connection
void setup() async {
getIt.registerSingleton<AppDatabase>(
await LocalDatabase.initiateConnection(),
);
}
Also, it’s important to separate your abstraction like above. The AppDatabase class is an abstract class, and the LocalDatabase is the implementation. This is done to make extending your app easier in the future.
Then, in your main.dart
you can change your main()
method to be asynchronous and wait for the setup to be finished.
void main() async {
await setup();
runApp(...);
}
Then, you can access your registered classes inside getIt
anywhere in your app:
class WriterCubit extends Cubit<WriterState> {
WriterCubit(this.db = getIt<AppDatabase>()): super(LazyState())
final AppDatabase db;
}
Lazy Load GetIt
If your project gets larger and your list of classes inside GetIt gets bigger, then you probably want to only instantiate a class when it’s needed to save some start-up time.
To do that, you can lazily register a class to GetIt so that it will be instantiated if needed, not at the start-up.
GetIt.instance.registerLazySingleton<AppRepository>( () => LocalRepository() )
Modularizing GetIt
Modularizing means that we can have multiple instances of GetIt with different names. This is useful if we have, for example, both local and internet-based sources of data. Also, it can be used to register an already registered class to GetIt as a different instance.
This is how we create multiple instances of GetIt:
final instanceName = "local";
GetIt.instance.registerSingleton<GetIt>(
GetIt.instance,
instanceName: instanceName,
);
return GetIt.I<GetIt>(instanceName: instanceName);
That’s all for this article. Feel free to point out in the comment any point that you feel incorrect or could be included in this article. See ya!