Explore GetIt In Flutter
The flutter widget is built using a modern framework. It is like a reaction. In this, we start with the widget to create any application. Each component in the screen is a widget. The widget describes what his outlook should be given his present configuration and condition. Keeping your widgets from having direct dependencies makes your code better organized and easier to test and maintain. But now you need a way to access these objects from your UI code. When I came to Flutter from the. Widget shows were similar to its idea and current setup and state. Flutter is a free and open-source tool to develop mobile, desktop, web applications with a single code base.
In this article, we will explain what is GetIt in flutter using the flutter_get_it_package. With the help of the package, and how to use them in your flutter applications. So let’s get started.
Table Of Contents :
GetIt
Implementation
Code Implement
Code File
Conclusion
Flutter :
“ Flutter is Google’s UI toolkit that helps you build beautiful and natively combined applications for mobile, web, and desktop in a single codebase in record time, Flutter offers great developer tools, with amazing hot reload”
GetIt :
GetIt package is a kind of this simple service locator in which some you will have a central registry whereby registering the class then we can get an instance of that class we use it instead of inherited widget or provider we use to access the object Is. from your UI.
Service Locator, dependency injection Both are forms of inversion of control (IOC). An IOC allows requests from anywhere from registering its class type to accessing it to the container.
Implementation:
Step 1: Add the dependencies
Add dependencies to pubspec — yaml file.
dependencies :
dependencies:
get_it: ^7.2.0
Step 2: Importing
import 'package:get_it/get_it.dart';
Step 3: Enable AndriodX
org.gradle.jvmargs=-Xmx1536M
android.enableR8=true
android.useAndroidX=true
android.enableJetifier=true
Code Implementation:
Before explaining about GetIt we will make a method of GitIt to use in our code as given in reference below.
This is our service locator.
GetIt getIt = GetIt.instance;
Next, we have created an abstract class named GetItAppModel which extends ChangeNotifier.
abstract class GetItAppModel extends ChangeNotifier {
void incrementCounter();
int get counter;
}