当前位置:网站首页>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
CustomerScrollView
ScrollView
CustomerScrollView
Sliver
CustomScrollView
slivers
- title : Use
Container
Wrap 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
Sliver
Details view times update

http://localhost:3900/api/dynamics/view/:id
if (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
GetIt
Provider
Map
// 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.dart
DynamicListener
_DynamicPageState
import '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 .
initState
class _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
dynamicAdded
Method to update the list page ;
- Edit page : After successful editing
dynamicUpdated
Method to update the list page ;
- Details page : After updating the number of views, call
dynamicUpdated
Method 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...catch
Code 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
边栏推荐
- Web17 -- use of El and JSTL
- Jiachen chapter Genesis "inner universe" joint Edition
- SSM框架系列——Junit单元测试优化day2-3
- Flash project cross domain interception and DBM database learning [Baotou cultural and creative website development]
- Uni app native app local packaging integrated Aurora push (jg-jpush) detailed tutorial
- [csnote] ER diagram
- S2-062 远程命令执行漏洞复现(cve-2021-31805)
- 【每日一题】棋盘问题
- Packet capturing and sorting -- TCP protocol [8]
- [wechat applet] Z-index is invalid
猜你喜欢
Kubernetes 入門教程
Bert base Chinese Download (SMART)
没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
leetcode:437. Path sum III [DFS selected or not selected?]
Idea的src子文件下无法创建servlet
[unity note] basic lighting in l4unity
leetcode:437. 路径总和 III【dfs 选还是不选?】
box-sizing
Plato Farm-以柏拉图为目标的农场元宇宙游戏
SSM framework series - annotation development day2-2
随机推荐
【蓝桥杯】4月17日省赛刷题训练(前3道题)
Use source insight to view and edit source code
leetcode-791. Custom string sorting
NBIOT的AT指令
S2-062 远程命令执行漏洞复现(cve-2021-31805)
21 days learning mongodb notes
Markdown语法学习
Please help me see what this is, mysql5 5. Thanks
云原生KubeSphere部署Mysql
unity常见的问题(一)
Plato farm - a game of farm metauniverse with Plato as the goal
软件测试周刊(第68期):解决棘手问题的最上乘方法是:静观其变,顺水推舟。
梳理网络IP代理的几大用途
Web17——EL与JSTL的使用
flask项目跨域拦截处理以及dbm数据库学习【包头文创网站开发】
How does sqlserver insert or update the number of weeks of the day instead of text
没有空闲服务器?导入 OVF 镜像快速体验 SmartX 超融合社区版
BaseRecyclerViewAdapterHelper 实现下拉刷新和上拉加载
Aviation core technology sharing | overview of safety characteristics of acm32 MCU
SSM框架系列——注解开发day2-2