简体   繁体   English

在 Android Studio 中生成 json_serializable(Flutter/Dart 插件)样板代码的快捷方式

[英]Shortcut for generating json_serializable (Flutter/Dart plugin) boilerplate codes in Android Studio

json_serializable plugin of Dart, does a great job of automatically generating some error prone and cumbersome parts of code, in exchange for some boilerplate: two methods, one annotation, and one reference to the generated file. Dart的 json_serializable 插件,在自动生成一些容易出错和繁琐的代码部分方面做得很好,以换取一些样板:两个方法,一个注释和一个对生成文件的引用。

import 'package:json_annotation/json_annotation.dart';

part 'location.g.dart';

@JsonSerializable()
class Location {
  final double lat;
  final double lng;

  Location(this.lat, this.lng);

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

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

Obviously this better be done also by the machine, like the constructor for this class: I just write the final field, then press alt+enter and Android Studio places the constructor for me.显然这也最好由机器来完成,就像这个 class 的构造函数:我只写最后一个字段,然后按 alt+enter 和 Android Studio 为我放置构造函数。

Does someone know how to make Android Studio do that for json_serializable?有人知道如何让 Android Studio 为 json_serializable 做到这一点吗?

There's a Visual Studio Code extension called Dart Data Class Generator ( https://marketplace.visualstudio.com/items?itemName=BendixMa.dart-data-class-generator ) that can be given either the list of final fields, or even a sample JSON file, and it will generate the Data Class complete with many useful methods. There's a Visual Studio Code extension called Dart Data Class Generator ( https://marketplace.visualstudio.com/items?itemName=BendixMa.dart-data-class-generator ) that can be given either the list of final fields, or even a示例 JSON 文件,它将生成包含许多有用方法的数据 Class。 It claims:它声称:

The generator can generate the constructor, copyWith, toMap, fromMap, toJson, fromJson, toString, operator == and hashCode methods for a class based on class properties or raw JSON.生成器可以基于 class 属性或原始 Z0ECD11C1D7A287401D148A23BBDA 为 class 生成构造函数、copyWith、toMap、fromMap、toJson、fromJson、toString、operator == 和 hashCode 方法。

If you're on the Java side of things, I also see: https://plugins.jetbrains.com/plugin/12429-dart-data-class but I haven't played with that.如果您在 Java 方面,我还看到: https://plugins.jetbrains.com/plugin/12429-dart-data-class但我没有玩过。

I finally wrote this simple Live Template script.我终于写了这个简单的 Live Template 脚本。 You just have to enter the file name and list of fields.您只需输入文件名和字段列表。 See the gif below.请参阅下面的 gif。

import 'package:json_annotation/json_annotation.dart';

part '$NAME$.g.dart'

@JsonSerializable(explicitToJson: true)
class $CAP_NAME$ {
  $END$
  
  $CAP_NAME$();
  
  factory $CAP_NAME$.fromJson(Map<String, dynamic> json) => _$$$CAP_NAME$FromJson(json);
  
  Map<String, dynamic> toJson() => _$$$CAP_NAME$ToJson(this);
}

演示

Well, in this simple solution the mentioned boiler plate is now being generated, and this works fine for me, but it is a very naive way indeed, I didn't invest much time in learning the Live Template script.好吧,在这个简单的解决方案中,现在正在生成提到的样板,这对我来说很好,但这确实是一种非常幼稚的方式,我没有花太多时间学习 Live Template 脚本。 One improvement is to make it write the fields in the constructor parameter list instead of doing it manually.一项改进是让它在构造函数参数列表中写入字段,而不是手动执行。 Another way is to use the File Template script, which I did not look into, and possibly create the file and fields in a dialog.另一种方法是使用我没有研究的文件模板脚本,并可能在对话框中创建文件和字段。

Install "Dart json serialization generator" plugins in android studio.在 android studio 中安装“Dart json 序列化生成器”插件。 I will create the option.我将创建选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM