flutter开发实战-JSON和序列化数据

flutter开发实战-JSON和序列化数据

大多数移动应用都需要与 web 服务器通信,同时在某些时候轻松地存储结构化数据。当创造需要网络连接的应用时,它迟早需要处理一些常见的 JSON。使用Json时候,可以使用json_serializable

一、引入json_annotation以及dev build_runner

运行 flutter pub add 添加依赖:

复制代码
$ flutter pub add json_annotation dev:build_runner dev:json_serializable

例如:

复制代码
dependencies:
  flutter:
    sdk: flutter

  # JSON注解
  json_annotation: ^4.8.0
  
dev_dependencies:
  flutter_test:
    sdk: flutter

  # JSON转model
  json_serializable: ^6.6.1
  build_runner: ^2.3.3

我们可以使用网址:https://caijinglong.github.io/json2dart/index.html

假如,我们有Json如下

复制代码
{
"name":"名称",
"id":1,
"age":23
}

通过网址可以帮我们生成代码

复制代码
import 'package:json_annotation/json_annotation.dart'; 
  
part 'student_info.g.dart';


@JsonSerializable()
  class StudentInfo extends Object {

  @JsonKey(name: 'name')
  String name;

  @JsonKey(name: 'id')
  int id;

  @JsonKey(name: 'age')
  int age;

  StudentInfo(this.name,this.id,this.age,);

  factory StudentInfo.fromJson(Map<String, dynamic> srcJson) => _$StudentInfoFromJson(srcJson);

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

}

当然,生成的代码类属性没有做可能为null的判断,需要更改成如下,即每个属性前增加?,这里是英文的哦

复制代码
import 'package:json_annotation/json_annotation.dart'; 
  
part 'student_info.g.dart';


@JsonSerializable()
  class StudentInfo extends Object {

  @JsonKey(name: 'name')
  String? name;

  @JsonKey(name: 'id')
  int? id;

  @JsonKey(name: 'age')
  int? age;

  StudentInfo(this.name,this.id,this.age,);

  factory StudentInfo.fromJson(Map<String, dynamic> srcJson) => _$StudentInfoFromJson(srcJson);

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

}

二、通过命令生成对应类

通过命令帮我们生成student_info.g.dart

复制代码
flutter packages pub run build_runner build

最后帮我们生成的student_info.g.dart代码如下

复制代码
// GENERATED CODE - DO NOT MODIFY BY HAND

part of 'student_info.dart';

// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************

StudentInfo _$StudentInfoFromJson(Map<String, dynamic> json) =>
    StudentInfo(
      json['name'] as String?,
      json['id'] as int?,
      json['age'] as int?,
    );

Map<String, dynamic> _$StudentInfoToJson(StudentInfo instance) =>
    <String, dynamic>{
      'name': instance. name,
      'id': instance.id,
      'age': instance.age,
    };

三、flutter中无反射

Flutter 中是否有 GSON/Jackson/Moshi 的等价物?

答案是:无。

简单来说,没有。

这样的库需要使用运行时进行 反射,这在 Flutter 中是被禁用的。运行时反射会影响 Dart 支持了相当久的 摇树优化。通过 tree shaking,你可以从你的发布版本中"抖掉"不需要使用的代码。这会显著优化 App 的体积。

由于反射会默认让所有的代码被隐式使用,这让 tree shaking 变得困难。工具不知道哪一部分在运行时不会被用到,所以冗余的代码很难被清除。当使用反射时,App 的体积不能被轻易优化。

尽管你不能在 Flutter 中使用运行时反射,还是有一些库提供了基于代码生成的方便使用的 API,这个方法的更多细节在 代码生成库 部分。

来自:https://docs.flutter.cn/data-and-backend/serialization/json

四、小结

flutter开发实战-JSON和序列化数据

学习记录,每天不停进步。

相关推荐
浮江雾32 分钟前
Flutter第四节------核心概念学习笔记:从基础语法到异步编程
linux·服务器·开发语言·笔记·flutter·入门·基础
心中有国也有家1 小时前
AtomGit Flutter 鸿蒙客户端:Flutter 鸿蒙应用的错误处理与优雅降级策略
学习·flutter·华为·harmonyos
心中有国也有家13 小时前
AtomGit Flutter 鸿蒙客户端: ChangeNotifier 模式
学习·flutter·华为·harmonyos
liguojun202518 小时前
篮球馆自动计时收费系统:从规则配置到自动结算的全流程拆解
java·大数据·运维·人工智能·物联网·1024程序员节
浮江雾1 天前
Flutter第三节----Dart中的数据类型
android·开发语言·学习·flutter·入门·函数
又菜又爱coding1 天前
Flutter Android 无线调试No supported devices connected
flutter
心中有国也有家1 天前
AtomGit Flutter 鸿蒙客户端:初始化流水线
学习·flutter·华为·harmonyos
ZZZMMM.zip2 天前
基于鸿蒙HarmonyOS NEXT开发AI英语口语应用:智能口语练习新体验与鸿蒙Flutter框架跨端实
人工智能·flutter·华为·harmonyos·鸿蒙·鸿蒙系统
心中有国也有家2 天前
AtomGit Flutter 鸿蒙客户端:白噪音场景的视觉设计
学习·flutter·华为·harmonyos
心中有国也有家2 天前
AtomGit Flutter 鸿蒙客户端:呼吸练习的完整生命周期
学习·flutter·华为·harmonyos