“time” : 1175714200,
“title” : “My YC app: Dropbox - Throw away your USB drive”,
“type” : “story”,
“url” : “http://www.getdropbox.com/u/2/screencast.html”
}‘’';
}
第二步:添加依赖
在pubspec.yaml中添加如下依赖
dependencies:
Your other regular dependencies here
json_annotation: ^1.2.0
dev_dependencies:
Your other dev_dependencies here
build_runner: ^0.10.2
json_serializable: ^1.5.1
这里需要添加三个依赖,它们分别是:“json_annotation” “build_runner” 和 “json_serializable”。
请注意,yaml配置文件对于缩进要求十分严格,下面的build_runner和json_serializable应该是与flutter_test平级的,千万不要写在flutter_test缩进后,这样它会认为这两个是flutter_test的子集目录!
由于很多朋友在这一步遇到了问题,这里贴出源码
第三步:根据json创建实体类
我们这里根据上面的json数据写好了一个dart的实体类
class Data{
final String by;
final int descendants;
final int id;
final List kids;
final int score;
final int time;
final String title;
final String type;
final String url;
Data({this.by, this.descendants, this.id, this.kids, this.score, this.time,
this.title, this.type, this.url});
}
我们在这里使用了dart语法糖创建了构造函数。具体请参考(www.dartlang.org/guides/lang…)。
第四步:关联实体类文件
我们需要在我们的实体类中关联生成文件。
import ‘package:json_annotation/json_annotation.dart’;
part ‘data.g.dart’;
@JsonSerializable()
class Data {
final String by;
final int descendants;
final int id;
final List kids;
final int score;
final int time;
final String title;
final String type;
@JsonKey(nullable: false)
final String url;
Data({this.by, this.descendants, this.id, this.kids, this.score, this.time,
this.title, this.type, this.url});
刚写完data.g.dart的会报错,这是正常的!因为我们还没生成解析文件
- 为了使实体类文件找到生成文件,我们需要 part ‘data.g.dart’。
第五步:生成Json解析文件
当当当…!这里开始就是重头戏了!!
我们要使用JsonSerializable生成代码的话必须要在需要生成代码的实体类前添加注解@JsonSerializable(),而要使用这个注解我们必须引入json_annotation/json_annotation.dart这个包。
import ‘package:json_annotation/json_annotation.dart’;
@JsonSerializable()
class Data{
final String by;
final int descendants;
final int id;
final List kids;
final int score;
final int time;
final String title;
final String type;
final String url;
Data({this.by, this.descendants, this.id, this.kids, this.score, this.time,
this.title, this.type, this.url});
}
这里需要注意,flutter编码规范dart文件名统一小写,这样可以避免很多问题。ok这样实体类就创建完成啦。
那么问题来了,应该如何生成代码呢?
还记得我们刚才添加的build_runner的依赖吗,这时候我们就需要它来帮忙咯。
build_runner
build_runner是dart团队提供的一个生成dart代码文件的外部包。
我们在当前项目的目录下运行flutter packages pub run build_runner build
运行成功后我们应该能在这个实体类的下面发现一个新的文件
这个data.g.dart就是build_runner根据JsonSerializable生成的json解析文件。 我们来看看这个生成的dart文件
// GENERATED CODE - DO NOT MODIFY BY HAND
part of ‘data.dart’;
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
Data _$DataFromJson(Map<String, dynamic> json) {
return Data(
by: json[‘by’] as String,
descendants: json[‘descendants’] as int,
id: json[‘id’] as int,
kids: (json[‘kids’] as List)?.map((e) => e as int)?.toList(),
score: json[‘score’] as int,
time: json[‘time’] as int,
title: json[‘title’] as String,
type: json[‘type’] as String,
url: json[‘url’] as String);
}
Map<String, dynamic> _$DataToJson(Data instance) => <String, dynamic>{
‘by’: instance.by,
‘descendants’: instance.descendants,
‘id’: instance.id,
‘kids’: instance.kids,
‘score’: instance.score,
‘time’: instance.time,
‘title’: instance.title,
‘type’: instance.type,
‘url’: instance.url
};
同志们请注意这段代码最上面的注释"// GENERATED CODE - DO NOT MODIFY BY HAND"。你可千万别手写生成文件啊哈哈哈哈。
这段代码生成实体类库的一个part,在老版本part中有一个抽象实体类的mixin,dart中使用基于mixin的继承来解决单继承的局限性。
新版本中声称了两个方法,fromJson和toJson方法,它们能干什么相信大家从名字上就能猜到了。
【对part感兴趣的同学可以参考https://juejin.im/post/6844903649617936392#heading-8 Dart | 浅析dart中库的导入与拆分。
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
总结
最后为了帮助大家深刻理解Android相关知识点的原理以及面试相关知识,这里放上相关的我搜集整理的14套腾讯、字节跳动、阿里、百度等2021最新面试真题解析,我把技术点整理成了视频和PDF(实际上比预期多花了不少精力),包知识脉络 + 诸多细节。
网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。
一个人可以走的很快,但一群人才能走的更远。不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎扫码加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!
[外链图片转存中…(img-mzyKaZfJ-1712632421320)]