当前位置:网站首页>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-
边栏推荐
- Which C4d cloud rendering platform to cooperate with?
- [Vani有约会]雨天的尾巴
- 在Perl程序中暴露Prometheus指标
- Basic functions and collections of guava
- Li Mu hands-on learning, in-depth learning, V2 transformer and code implementation
- 杂谈:最近好多朋友谈出国……
- Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较
- Want to sink into redis hash and write in the attributes and values of the object. Do the bosses have a demo?
- Esp8266 (esp-12f) third party library use -- sparkfun_ Apds9960 (gesture recognition)
- LogCat工具
猜你喜欢
随机推荐
Using loops to process data in tables in kettle
VLAN trunk实验
Jmeter:接口自动化测试-BeanShell对数据库数据和返回数据比较
Turn off the auto start function of Oracle service in centos7
整体二分?
Excuse me, MySQL timestamp (6) using flick SQL is null. Is there a way to deal with this
The solution of using sqlplus to display Chinese as garbled code
Firefox browser, when accessing Tencent cloud server, failed to establish a secure connection.
Haikang H9 camera cannot be connected with xshell (SSH is not enabled)
临界区(critical section 每个线程中访问 临界资源 的那段代码)和互斥锁(mutex)的区别(进程间互斥量、共享内存、虚拟地址)
Guava的基础功能与集合
Want to sink into redis hash and write in the attributes and values of the object. Do the bosses have a demo?
oracle的触发器的使用举例
Demonstrate the use of foreign keys with Oracle
用户解锁SM04 SM12
C语言程序设计 | 程序编译与预处理
36 - new promise method: allsettled & any & race
2022 0726 Gu Yujia's study notes
网络入门——vlan及trunk概述
2022-07-25 Gu Yujia's study notes









