Flutter开发之——json序列化(1)

factory ArticleData.fromJson(Map<String, dynamic> json) => _$ArticleDataFromJson(json);

Map<String, dynamic> toJson() => _$ArticleDataToJson(this);

}

@JsonSerializable()

class Person {

String apkLink;

int audit;

String author;

bool canEdit;

int chapterId;

String chapterName;

bool collect;

int courseId;

String desc;

String descMd;

String envelopePic;

bool fresh;

String host;

int id;

String link;

String niceDate;

String niceShareDate;

String origin;

String prefix;

String projectLink;

int publishTime;

int realSuperChapterId;

int selfVisible;

int shareDate;

String shareUser;

int superChapterId;

String superChapterName;

List tags;

String title;

int type;

int userId;

int visible;

int zan;

Person(

this.apkLink,

this.audit,

this.author,

this.canEdit,

this.chapterId,

this.chapterName,

this.collect,

this.courseId,

this.desc,

this.descMd,

this.envelopePic,

this.fresh,

this.host,

this.id,

this.link,

this.niceDate,

this.niceShareDate,

this.origin,

this.prefix,

this.projectLink,

this.publishTime,

this.realSuperChapterId,

this.selfVisible,

this.shareDate,

this.shareUser,

this.superChapterId,

this.superChapterName,

this.tags,

this.title,

this.type,

this.userId,

this.visible,

this.zan);

factory Person.fromJson(Map<String, dynamic> json) => _$PersonFromJson(json);

Map<String, dynamic> toJson() => _$PersonToJson(this);

}

@JsonSerializable()

class Tag {

String name;

String url;

Tag(this.name, this.url);

factory Tag.fromJson(Map<String, dynamic> json) => _$TagFromJson(json);

Map<String, dynamic> toJson() => _$TagToJson(this);

}

注意:

  • 添加part 'Article.g.dart';其中:Article是dart文件名,文件名+g.dart格式

  • 在每个需要序列化的类前面添加@JsonSerializable(),如类:ArticleArticleDataPersonTag

  • 每个类里面有:变量,构造函数,类名.fromJson() => _$类名FromJson(json)toJson() => _$类名ToJson(this)

3.3 生成序列化类.g.dart文件

执行如下指令

flutter pub run build_runner build

如无错误发生,生成Article.g.dart文件,Article.dart文件中_$类名FromJson(json)等不会再出现错误

四 序列化后类的应用


4.1 代码

late Future

futureArticle;

@override

void initState() {

super.initState();

futureArticle=_httpGet2();

}

Future

_httpGet2() async {

var httpClient = new HttpClient();

var uri = Uri(

scheme: ‘https’,

host: ‘www.wanandroid.com’,

path: ‘article/list/0/json’,

);

HttpClientRequest request = await httpClient.getUrl(uri);

HttpClientResponse response = await request.close();

String responseBody = await response.transform(utf8.decoder).join();

var jsonDecode = json.decode(responseBody);

var article =Article.fromJson(jsonDecode);

print(article);

return article;

}

//显示

FutureBuilder

(

future: futureArticle,

builder: (context, snapshot) {

if (snapshot.hasData) {

return ListView.separated(

itemCount: snapshot.data!.data.datas.length,

separatorBuilder: (context, index) {return Divider(height: 10, color: Colors.grey,);},

itemBuilder: (BuildContext context, int index) {

return Column(

children: [

Text(“${snapshot.data!.data.datas[index].author}”),
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

写在最后

本次我的分享也接近尾声了,感谢你们在百忙中花上一下午来这里聆听我的宣讲,希望在接下来的日子,我们共同成长,一起进步!!!

最后放上一个大概的Android学习方向及思路(详细的内容太多了~),提供给大家:

对于程序员来说,要学习的知识内容、技术有太多太多,这里就先放上一部分,其他的内容有机会在后面的文章向大家呈现出来,不过我自己所有的学习资料都整理成了一个文档,一直在不断学习,希望能帮助到大家,也节省大家在网上搜索资料的时间来学习,也可以分享动态给身边好友一起学习!

为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!

Android架构师之路很漫长,一起共勉吧!

如果你觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

起学习!

为什么某些人会一直比你优秀,是因为他本身就很优秀还一直在持续努力变得更优秀,而你是不是还在满足于现状内心在窃喜!希望读到这的您能点个小赞和关注下我,以后还会更新技术干货,谢谢您的支持!

Android架构师之路很漫长,一起共勉吧!

如果你觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言,一定会认真查询,修正不足,谢谢。

[外链图片转存中…(img-yhxYK6UL-1712760233083)]

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 30
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值