original
https://medium.com/flutterdevs/explore-getit-in-flutter-8db723e9d7cf
refer to
text
Its Flutter widget is built using a modern framework. This is like a reaction. Here, we start with widgets to create any application. Each component on the screen is a small part. This widget describes what his prospects should look like based on his current configuration and status. Keeping your widgets without direct dependencies can make your code better organized and easier to test and maintain. But now you need a way to access these objects from UI code. When I came to Flutter from. The widget display is similar to its idea and current settings and status. Flutter is a free and open source tool for developing mobile, desktop, and web applications. Only one code base is required.
In this article, we will use Flutter to obtain its packaging to illustrate what Flutter obtains it. With the help of packages, and how to use them in your Flutter application. So let's get started.
Flutter :
Flutter is Google's user interface toolkit, which can help you build beautiful, locally combined applications for mobile devices, the web, and the desktop in record time. Flutter provides great development tools with amazing hot reload performance
Return to the article page to fix it:
A software package is such a simple service locator. In this service locator, you have a central registry. By registering the class, we can get an instance of the class. We use it instead of inherited widgets or providers. To access the object Is. From your user interface.
Service locator and dependency injection are both forms of inversion of control. IOC allows requests from anywhere, from registering its class type to accessing the container.
implementation plan:
Step 1: add dependencies
Add dependencies to the pubspec ーyaml file.
Dependency:
dependencies:
get_it: ^7.2.0
Step 2: Import
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 GetIt, we will give a GitIt method used in our code in the reference below.
This is our service locator.
GetIt getIt = GetIt._instance_;
Next, we created an abstract class called getappmodel, which extends ChangeNotifier.
abstract class GetItAppModel extends ChangeNotifier {
void incrementCounter();
int get counter;
}
Now that we have created a class called getappmodelimplementation, which extends from the getappmodel class, we have created the incrementCounter () method, which increments the counter value.
class GetItAppModelImplementation extends GetItAppModel {
int _counter = 0;
GetItAppModelImplementation() {
Future.delayed(Duration(seconds: 3)).then((_) => getIt.signalReady(this));
}
@override
int get counter => _counter;
@override
void incrementCounter() {
_counter++;
notifyListeners();
}
}
After that, we will create a counter application. In this program, we have obtained two text parts inside, and the outside is floatingActionButton. Clicking it will call the getappmodel () class. In the counter application, the value will be Add to the above column part, the column part is wrapped by FutureBuilder and getIt.allReady () and defined in the future attribute.
FutureBuilder(
future: getIt.allReady(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
getIt<GetItAppModel>().counter.toString(),
style: Theme._of_(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: getIt<GetItAppModel>().incrementCounter,
tooltip: 'Increment',
child: Icon(Icons._add_),
),
);
} else {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text('Initialisation'),
SizedBox(
height: 16,
),
CircularProgressIndicator(),
],
);
}
})
Full code:
import 'package:flutter/material.dart';
import 'package:flutter_getit_exm/app_model.dart';
import 'package:flutter_getit_exm/main.dart';
class GetItExm extends StatefulWidget {
GetItExm({Key? key, required this.title}) : super(key: key);
final String title;
@override
_GetItExmState createState() => _GetItExmState();
}
class _GetItExmState extends State<GetItExm> {
@override
void initState() {
getIt
.isReady<GetItAppModel>()
.then((_) => getIt<GetItAppModel>().addListener(update));
super.initState();
}
@override
void dispose() {
getIt<GetItAppModel>().removeListener(update);
super.dispose();
}
void update() => setState(() => {});
@override
Widget build(BuildContext context) {
return Material(
child: FutureBuilder(
future: getIt.allReady(),
builder: (context, snapshot) {
if (snapshot.hasData) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button this many times:',
),
Text(
getIt<GetItAppModel>().counter.toString(),
style: Theme._of_(context).textTheme.headline4,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: getIt<GetItAppModel>().incrementCounter,
tooltip: 'Increment',
child: Icon(Icons._add_),
),
);
} else {
return Column(
mainAxisAlignment: MainAxisAlignment.center,
mainAxisSize: MainAxisSize.min,
children: [
Text('Waiting for initialisation'),
SizedBox(
height: 16,
),
CircularProgressIndicator(),
],
);
}
}),
);
}
}
Conclusion:
In this article, I explained Explore GetIt In Flutter, you can modify and experiment according to your own, this small introduction comes from Explore GetIt In Flutter's demo from our side.
I hope this blog will provide you with sufficient information, while trying to explore GetIt in Flutter in your Flutter project. We showed you what exploration is and what Flutter is working in your Flutter app, so please try it.
© Cat brother
- https://ducafecat.tech/
- https://github.com/ducafecat
- WeChat group ducafecat
- b station https://space.bilibili.com/404904528
Past
Open source
GetX Quick Start
https://github.com/ducafecat/getx_quick_start
News client
https://github.com/ducafecat/flutter_learn_news
strapi manual translation
WeChat discussion group ducafecat
Series collection
Translation
https://ducafecat.tech/categories/%E8%AF%91%E6%96%87/
Open source project
https://ducafecat.tech/categories/%E5%BC%80%E6%BA%90/
Dart programming language basics
https://space.bilibili.com/404904528/channel/detail?cid=111585
Getting started with Flutter zero foundation
https://space.bilibili.com/404904528/channel/detail?cid=123470
Flutter actual combat from scratch news client
https://space.bilibili.com/404904528/channel/detail?cid=106755
Flutter component development
https://space.bilibili.com/404904528/channel/detail?cid=144262
Flutter Bloc
https://space.bilibili.com/404904528/channel/detail?cid=177519
Flutter Getx4
https://space.bilibili.com/404904528/channel/detail?cid=177514
Docker Yapi
https://space.bilibili.com/404904528/channel/detail?cid=130578
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。