当前位置:网站首页>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)

The first time to tear the code by hand, how to solve the problem of full arrangement

面试知识点整理:Skia 架构的场景渲染

二舅为什么能刷屏?这三件事对企业公关的启示

Solve the problem that the 5+APP real machine test cannot access the background (same local area network)

对账、结账、错账更正方法、划线更正法、红字更正法、补充登记法

How to determine the direction based on two coordinate points on the map

解密:链动2+1的商业模式

解决flex布局warp自动换行下最后一行居中问题

File upload vulnerability
随机推荐
Debian 12 Bookworm 尝鲜记
Syncthing文件同步方案完全攻略(亲测有效)
英语每日打卡
面试必备:Android性能分析与优化实战进阶手册
php laravel框架生成二维码
云安全笔记:云原生全链路加密
对账、结账、错账更正方法、划线更正法、红字更正法、补充登记法
pytorch:保存和加载模型
学IT,找工作——移除链表元素
file contains vulnerabilities
学IT,找工作——反转链表
kotlin语法总结(二)
修复APP的BUG,热修复的知识点和大厂的相关资料汇总
Anaconda报错:An unexpected error has occurred. Conda has prepared the above report 解决办法
3000字入门图神经网络
管理会计(对内)指引、管理会计要素及其具体内容(可能考,考前记一下,推荐记一下四个大点即可)、
一次代码审计的笔记(CVE-2018-12613 phpmyadmin文件包含漏洞)
Dcat Admin 关闭代码生成器 登录指定地址
二舅为什么能刷屏?这三件事对企业公关的启示
Windows下MySQL数据库报“ERROR 2003 (HY000): Can‘t connect to MySQL server on ‘localhost:8000‘ (10061)”错误解决