当前位置:网站首页>Actual combat of flutter - Request encapsulation (I)
Actual combat of flutter - Request encapsulation (I)
2022-07-27 07:29:00 【Blue faced scholar】

It took two years flutter, Have some experience , Some cases will be updated from today , Not empty head and brain , Only for practical use , For study or use flutter My little friend's reference , Learning is still shallow , If there is anything wrong, we hope all gods will correct it , So as not to mislead others , Thank you here. ~( Originality is not easy. , Please indicate the source and author when forwarding )
Be careful : No special instructions ,flutter Version is 3.0+
Development of all programs , Including native development ,web front end , Small programs inevitably do one thing, that is, encapsulate the requests that interact with the background , Then it is convenient to receive data , So let's take it step by step today flutter The package of is written clearly .
One . Module partition
A good project structure should be clearly divided into modules , Include api request , for instance

such as base Based on api, Include login , register , Set interfaces .bi Interface for report statistics , And so on , goods , Orders and so on .
Two . Request to packaging
Let's first look at the packaging requirements
1. The request url Defined in the head , Easy to find and modify
2. Encapsulation request method HttpUtils( We will talk about this in a new chapter )
3. Define the entity classes requested and returned . There are several cases of general interface return
- Return information such as success , The result could be String,bool
- Return an entity class , For example, user information UserDo
- Return the list of entity classes ,List<Object>, Enter and return the permission list of the login user
Let's look at a simple example of three types :
class UserApi {
/// Query login user
static const String _getUser = "/base/login/user";/// Sign in
static const String _login ="/base/login";// Query login permission
static const _selectPermission = "/base/selectPermission";
static Future<UserDo> getUser() async {
var res = await HttpUtils.get(_getUser, baseUrl: Config.BASE_API_URL);
return UserDo.fromJson(res);
}static Future<String> login(dynamic param) async {
var res = await HttpUtils.post(_login, data: param, baseUrl: Config.BASE_API_URL, showLoading: showLoad);
return res;
}static Future<List<Permission>> selectPermission(List<String> permissions) async {
var res = await HttpUtils.post(
_selectPermission,
data: permissions,
baseUrl: Config.BASE_API_URL,
);
returnres.map<Permission>((e) => Permission.fromJson(e)).toList();
}
3、 ... and .Json Handle :FlutterJsonBeanFactory
Dart Language in Json The aspect framework of transformation is not mature , There is no such thing Java And other language reflection mechanisms , If you manually process the conversion of the returned results , It will be a very troublesome thing , Fortunately, there are still many great gods , Here is a recommended conversion tool FlutterJsonBeanFactory, Can be generated quickly , And you can use the conversion method in the above example :fromJson and toJson.
Installation method :
Settings/Preferences > Plugins > Marketplace > Search for "FlutterJsonBeanFactory" > Install Plugin
The current use is 4.4.5 edition . Thank the author for upgrading all the way , Also witnessed the plug-in from if Judge , To Map The transformation of , More efficient search .
Use :
take PostMan Return result of , Copy directly in the creation window of the tool , According to the type
Generating entities

Of course, entity type nesting is also supported .
Conclusion :
Interested readers can study the source code by themselves .
Add in address :https://plugins.jetbrains.com/plugin/11415-flutterjsonbeanfactory-only-null-safety-
边栏推荐
- C# Winfrom 常用功能整合-2
- Pytorch notes: td3
- sql-labs SQL注入平台-第1关Less-1 GET - Error based - Single quotes - String(基于错误的GET单引号字符型注入)
- Oracle database problems
- Esp8266 (esp-12f) third party library use -- sparkfun_ Apds9960 (gesture recognition)
- Gossip: talk with your daughter about why you should learn culture lessons well
- 一个优先级顺序的SQL问题
- Use reflection to dynamically modify annotation attributes of @excel
- Oracle cleans up the Database disk space of tables with referenced partitions
- 网络入门——vlan及trunk概述
猜你喜欢

Properties类和properties配置文件的理解学习

C4D云渲染平台选哪家合作?

Which C4d cloud rendering platform to cooperate with?

MySQL2

Gossip: talk with your daughter about why you should learn culture lessons well

(2022牛客多校三)J-Journey(dijkstra)

Quartus: an error is reported when adding a.V file to someone else's project

(2022杭电多校三)1011.Taxi(曼哈顿最值+二分)

【WSL2】配置连接 USB 设备并使用主机的 USB 摄像头
![[wsl2] configure the USB camera connecting the USB device and using the host](/img/03/7ebc7eebeda1598c8f4fdd1e9188c7.png)
[wsl2] configure the USB camera connecting the USB device and using the host
随机推荐
用户解锁SM04 SM12
在kettle中快速更新一个字段中的信息
Port forwarding summary
Chapter 6 Shell Logic and Arithmetic
yhb_sysbench
在rhel8上使用soci连接oracle和postgresql和sqlite
单元测试系统化讲解之Mockito
Generics -- learn it, and there are many benefits
在mac中使用docker来搭建oracle数据库服务器
JS make a traffic light
2022 0726 Gu Yujia's study notes
Codeforces Round #810 (Div.2) A-C
Routing between VLANs (explanation + verification)
[Vani有约会]雨天的尾巴
(2022牛客多校三)A-Ancestor(LCA)
Tcp/ip protocol analysis (tcp/ip three handshakes & four waves + OSI & TCP / IP model)
Synchronized锁
在rhel7.3中编译和使用log4cxx
oracle的触发器的使用举例
IO中节点流和处理流的理解学习