当前位置:网站首页>Flutter入门之网络请求篇
Flutter入门之网络请求篇
2022-08-02 03:27:00 【翊兮】
本章主要介绍Flutter的网络请求相关操作
Dio:略!!!
Retrofit For Dart:
开局添加如下依赖
dependencies:
retrofit: ^1.3.4
logger: ^0.9.1
dev_dependencies:
retrofit_generator: ^1.3.4+2
build_runner: ^1.10.0
json_serializable: ^3.2.0
#注:json_serializable必须要添加 否则实体类不会自动生成然后就举个栗子
import 'package:json_annotation/json_annotation.dart';
part 'ResultVo.g.dart';
@JsonSerializable()
class ResultVo{
int code;
String msg;
dynamic obj;
ResultVo({this.code, this.msg, this.obj});
factory ResultVo.fromJson(Map<String, dynamic> json) => _$ResultVoFromJson(json);
Map<String, dynamic> toJson() => _$ResultVoToJson(this);
}之后 运行
flutter pub run build_runner build
ok
这里的意思就是
//固定格式
factory 类名.fromJson(Map<String, dynamic> json) =>_$类名FromJson(json);
Map<String, dynamic> toJson() => _$类名ToJson(this);
//配置好 part ‘类名.g.dart’
//添加序列化标注,@JsonSerializable(),注意括号
//新建类和属性(字段)以及带参构造方法,注意属性类型解析的话建议用下面的工具类解析完成后略作修改 就很nice
明明讲的是网络请求,干嘛要扯上json??? 最终都要解析的嘛....哈哈哈哈咳咳咳咳咳
关于Retrofit 其实跟上面也很类似
可直接CV下面示例
part 'RestClient.g.dart';
@RestApi(baseUrl: "http://xxx.xx.xx.xx/xxx/")
abstract class RestClient {
factory RestClient(Dio dio, {String baseUrl}) = _RestClient;
@POST("/my/login")
Future<ResultVo> teacher_login(@Field() String account,
@Field() String password);
}
var dio = Dio(BaseOptions(
connectTimeout: 5000,
receiveTimeout: 10000,
));
final client = RestClient(dio);
final logger = Logger();之后运行
flutter pub run build_runner build
稍等片刻结果看得见!!! 哈哈
更多Dart-JSON本地解析等参考
边栏推荐
- How to calculate the distance between two points on the earth (with formula derivation)
- 【泰山众筹】模式为什么一直都这么火热?是有原因的
- 借贷记账法下的账户结构、借贷记账法的记账规则、借贷记账法下的账户对应关系与会计分录
- 二舅为什么能刷屏?这三件事对企业公关的启示
- View的滑动
- SGDP(1)——猜数字游戏
- 阿里技术官手码12W字面试小册
- The first time to tear the code by hand, how to solve the problem of full arrangement
- 账务处理程序、记账凭证账务处理程序、汇总记账凭证账务处理程序、科目汇总表账务处理程序、会计信息化概述、信息化环境下会计账务处理的基本要求(此章出1道小题)
- xxe of CTF
猜你喜欢

ES6 iterator explanation example

laravel-admin 线上访问项目,一直重定向到登录页面

Activity

在 UUP dump 被墙的情况下如何用 UUP 下载 ISO 镜像

【一句话攻略】彻底理解JS中的回调(Callback)函数

A network security guinea pig's learning path - scripting of advanced usage of nmap

文件上传漏洞

VIKINGS: 1 vulnhub walkthrough

会计凭证概述、原始凭证、原始凭证的种类、原始凭证的基本内容、原始凭证的填制要求、原始凭证的审核

The learning path of a network security mouse - the basic use of nmap
随机推荐
WeChat applet development video loading: [Rendering layer network layer error] Failed to load media
By figure, a (complete code at the end)
Sensitive information leakage
Dcat Admin 关闭代码生成器 登录指定地址
VIKINGS: 1 vulnhub walkthrough
xxe of CTF
CSRF (Cross Site Request Forgery)
SGDP(2)——声纳寻宝游戏
Eric target penetration test complete tutorial
PHP deserialization vulnerability
Win10 解决AMD平台下SVM无法开启的问题
大厂底层必修:“应用程序与 AMS 的通讯实现”
hackmyvm-random walkthrough
元宇宙是一个炒作的科幻概念,还是互联网发展的下半场?
kotlin语法总结(二)
会计账簿、会计账簿概述、会计账簿的启用与登记要求、会计账簿的格式和登记方法
(2) Sequence structures, Boolean values of objects, selection structures, loop structures, lists, dictionaries, tuples, sets
synchronized锁原理详解
账务处理程序、记账凭证账务处理程序、汇总记账凭证账务处理程序、科目汇总表账务处理程序、会计信息化概述、信息化环境下会计账务处理的基本要求(此章出1道小题)
Solve the problem that the 5+APP real machine test cannot access the background (same local area network)