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和序列化数据

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

相关推荐
2501_9206276110 小时前
Flutter 框架跨平台鸿蒙开发 - 派对策划助手应用
flutter·华为·harmonyos
里欧跑得慢11 小时前
Flutter 组件 powersync_core 的适配 鸿蒙Harmony 实战 - 驾驭极致离线优先架构、实现鸿蒙端高性能 SQL 增量同步与数据安全治理方案
flutter·harmonyos·鸿蒙·openharmony·powersync_core
芙莉莲教你写代码12 小时前
Flutter 框架跨平台鸿蒙开发 - 网络安全学习应用
学习·web安全·flutter·华为·harmonyos
kobesdu16 小时前
ROS Flutter GUI App:跨平台机器人可视化控制界面使用记录
flutter·机器人·ros
芙莉莲教你写代码16 小时前
Flutter 框架跨平台鸿蒙开发 - 时区转换器应用
学习·flutter·华为·harmonyos
芙莉莲教你写代码16 小时前
Flutter 框架跨平台鸿蒙开发 - 冥想指导应用
flutter·华为·harmonyos
王码码203517 小时前
Flutter 三方库 preact_signals 的鸿蒙化适配指南 - 掌控极致信号响应、Signals 架构实战、鸿蒙级精密状态指控专家
flutter·harmonyos·鸿蒙·openharmony·preact_signals
芙莉莲教你写代码18 小时前
Flutter 框架跨平台鸿蒙开发 - 密码管理器应用
服务器·flutter·华为·harmonyos
阿桂有点桂20 小时前
Flutter打包的app增加签名
flutter
芙莉莲教你写代码20 小时前
Flutter 框架跨平台鸿蒙开发 - 谜语大全
flutter·华为·harmonyos