当前位置:网站首页>Synchronously update the newly added and edited data to the list
Synchronously update the newly added and edited data to the list
2022-04-23 12:50:00 【InfoQ】
Preface
Dio- Details page interface construction ;
- Update details view times interface implementation ;
- GetIt brief introduction ;
- Use GetIt Register global objects ;
- Use GetIt Realize data synchronization between pages .
Details page
CustomerScrollViewScrollViewCustomerScrollViewSliverCustomScrollView
slivers
- title : Use
ContainerWrap in order to adjust the layout .
- View times : Similar to the number of views of the list .
- picture : In order to avoid the picture occupying too high a height , Limit picture height to 240.
- Content : Similar to the title , It's just that the font is turned down 2 Number
slivers The contents of the subcomponents are used SliverToBoxAdapter Convert to SliverDetails view times update

http://localhost:3900/api/dynamics/view/:idif (response.statusCode == 200) {
setState(() {
_dynamicEntity = DynamicEntity.fromJson(response.data);
});
_updateViewCount();
}
//...
void _updateViewCount() async {
try {
var response = await DynamicService.updateViewCount(_dynamicEntity.id);
if (response.statusCode == 200) {
setState(() {
_dynamicEntity.viewCount = response.data['viewCount'];
GetIt.instance.get<DynamicListener>().dynamicUpdated(
_dynamicEntity.id,
_dynamicEntity,
);
});
}
} catch (e) {
print(e.toString());
}
}
GetIt brief introduction
GetItProviderMap// Registered object : It's usually a single case
GetIt.instance.registerSingleton<T>(T object);
// Lazy load mode registration
GetIt.instance.registerLazySingleton<T>(FactoryFunc<T> func)
// Get the object in the container
GetIt.instance.get<T>();
Register dynamic change listening object

dynamic_listener.dartDynamicListener_DynamicPageStateimport 'package:home_framework/models/dynamic_entity.dart';
abstract class DynamicListener {
void dynamicUpdated(String id, DynamicEntity updatedDynamic);
void dynamicAdded(DynamicEntity newDynamic);
}
- New response method : When there is a new dynamic , Insert the new dynamic at the beginning ;
- Update method : Replace old dynamic data with new dynamic data .
initStateclass _DynamicPageState extends State<DynamicPage> implements DynamicListener {
// ...
@override
void initState() {
super.initState();
// Sign up to GetIt Containers
GetIt.instance.registerSingleton<DynamicListener>(this);
}
void dynamicUpdated(String id, DynamicEntity updatedDynamic) {
int index = _listItems.indexWhere((element) => element.id == id);
if (index != -1) {
setState(() {
_listItems[index] = updatedDynamic;
});
}
}
void dynamicAdded(DynamicEntity newDynamic) {
setState(() {
_listItems.insert(0, newDynamic);
});
}
// ...
}
Inter page data update
- New page : Call after new success
dynamicAddedMethod to update the list page ;
- Edit page : After successful editing
dynamicUpdatedMethod to update the list page ;
- Details page : After updating the number of views, call
dynamicUpdatedMethod to update the list page .
// New page
var response = await DynamicService.post(newFormData);
if (response.statusCode == 200) {
Dialogs.showInfo(context, ' Add success ');
GetIt.instance
.get<DynamicListener>()
.dynamicAdded(DynamicEntity.fromJson(response.data));
Navigator.of(context).pop();
}
//-------------------------------------
// Edit page
if (response.statusCode == 200) {
Dialogs.showInfo(context, ' Saved successfully ');
// Process the successfully updated business
_handleUpdated(newFormData);
Navigator.of(context).pop();
}
// Processing updates , If the picture is updated, the dynamic picture content is updated
void _handleUpdated(Map<String, String> newFormData) {
_dynamicEntity.title = newFormData['title'];
_dynamicEntity.content = newFormData['content'];
if (newFormData.containsKey('imageUrl')) {
_dynamicEntity.imageUrl = newFormData['imageUrl'];
}
GetIt.instance.get<DynamicListener>().dynamicUpdated(
_dynamicEntity.id,
_dynamicEntity,
);
}
//-------------------------------------
// Details page
void _updateViewCount() async {
try {
var response = await DynamicService.updateViewCount(_dynamicEntity.id);
if (response.statusCode == 200) {
setState(() {
_dynamicEntity.viewCount = response.data['viewCount'];
GetIt.instance.get<DynamicListener>().dynamicUpdated(
_dynamicEntity.id,
_dynamicEntity,
);
});
}
} catch (e) {
print(e.toString());
}
}
Running effect

summary
- There's a lot of repetitive code : such as
try...catchCode block ;
- Exposed Dio The details of the ;
- The interface participates in the construction of business objects , No separation from business logic .

版权声明
本文为[InfoQ]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231248411191.html
边栏推荐
- 数据库中的日期时间类型
- 甲辰篇 創世紀《「內元宇宙」聯載》
- Redis deployment of cloud native kubesphere
- Calculate the past date and days online, and calculate the number of live days
- CGC: contractual graph clustering for community detection and tracking
- BUUCTF WEB [BUUCTF 2018]Online Tool
- [Blue Bridge Cup] April 17 provincial competition brushing training (the first three questions)
- 5 free audio material websites, recommended collection
- Trier les principales utilisations de l'Agent IP réseau
- SynchronousQueue 源码解析
猜你喜欢
![[csnote] ER diagram](/img/97/82e8c2183fcafda50950a953ca0955.png)
[csnote] ER diagram

Unlock openharmony technology day! The annual event is about to open!

STM32 control stepper motor (ULN2003 + 28byj)

A graphic designer's fantasy world | ones characters

力扣刷题之完全二叉树的节点个数

Trier les principales utilisations de l'Agent IP réseau

有趣的IDEA插件推荐,给你的开发工作增添色彩

Idea的src子文件下无法创建servlet

解锁OpenHarmony技术日!年度盛会,即将揭幕!

Image attribute of input: type attribute of fashion cloud learning -h5
随机推荐
STM32控制步进电机(ULN2003+28byj)
【蓝桥杯】4月17日省赛刷题训练(前3道题)
软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。
Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
C, calculation code of parameter points of two-dimensional Bezier curve
硬核解析Promise对象(这七个必会的常用API和七个关键问题你都了解吗?)
Packet capturing and sorting -- TCP protocol [8]
uni-app 原生APP-云打包集成极光推送(JG-JPUSH)详细教程
BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载
Web17 -- use of El and JSTL
航芯技术分享 | ACM32 MCU安全特性概述
SSM框架系列——Junit单元测试优化day2-3
Markdown语法学习
Message queuing overview
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
梳理网络IP代理的几大用途
对话PostgreSQL作者Bruce:“转行”是为了更好地前行
Realize several "Postures" in which a box is horizontally and vertically centered in the parent box
[vulnhub range] - DC2
SSL证书退款说明