Ashkan Sarlak
Ashkan Sarlak

Reputation: 7344

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.

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.

Does someone know how to make Android Studio do that for json_serializable?

Upvotes: 5

Views: 8185

Answers (4)

seyhmus gumus
seyhmus gumus

Reputation: 116

From Plugins Downlaod Dart data classes than right click in model class in generate you can generate

Upvotes: 0

Shaikot37
Shaikot37

Reputation: 49

Install "Dart json serialization generator" plugins in android studio. It will create the option.

Upvotes: 3

Ashkan Sarlak
Ashkan Sarlak

Reputation: 7344

I finally wrote this simple Live Template script. You just have to enter the file name and list of fields. See the gif below.

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);
}

demo

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. 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.

Upvotes: 3

Randal Schwartz
Randal Schwartz

Reputation: 44081

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. 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.

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.

Upvotes: 1

Related Questions