From JSON to Dart advanced
This library is designed to generate Flutter
friendly model classes following the flutter’s doc recommendation and Effective Dart: Style. Extention supports for both Serializing JSON manually and Serializing JSON using code generation libraries
this
”, “break
”, “class
”, etc) will produce syntax errors.To customise your classes is very easy. If you want fast create a simple class then just click enter to continue skip all methods. Otherwise build your own. For generate Freezed class and Json Serializable choose Code Generation.
Add serializing JSON using code generation libraries to pubspec.yaml
.
structure of the pubspec.yaml
dependencies:
# Your other regular dependencies here
json_annotation: <latest_version>
dev_dependencies:
# Your other dev_dependencies here
build_runner: <latest_version>
json_serializable: <latest_version>
Freezed requires three packages to generate json files to Freezed classes with a few clicks.
structure of the pubspec.yaml
dependencies:
# Your other regular dependencies here
freezed_annotation: <latest_version>
dev_dependencies:
# Your other dev_dependencies here
build_runner: <latest_version>
freezed: <latest_version>
Read more about how to install freezed.
All generated classes with Freezed will be @immutable
and support all methods like copyWith, toString, equality operator==
… See example:
@freezed
abstract class Todos with _$Todos {
const factory Todos({
int userId,
int id,
String title,
bool completed,
}) = _Todos;
factory Todos.fromJson(Map<String, dynamic> json) => _$TodosFromJson(json);
}
Freezed generator are useful for who work daily with coding. All you have to do is upgrade some values and Freezed will takecare of the rest. Your don’t need worry about that you have forget update parser to some method. More what you can do withFreezed read freezed documentation.
TIP: If you think that you have too much generated files you can look at tips by Freezed how to ignore lint warnings on generated files.
Equatable
are immutable class with ability to compare your generated models in a better way. You can check if 2 classes, that are diferent instances, are equals without a single line of extra code. Ofcourse you can add toString method and copyWith for better experience.
class Todos extends Equatable {
final int userId;
final int id;
final String title;
final bool completed;
const Todos({
this.userId,
this.id,
this.title,
this.completed,
});
factory Todos.fromJson(Map<String, dynamic> json) {
return Todos(
userId: json['userId'] as int,
id: json['id'] as int,
title: json['title'] as String,
completed: json['completed'] as bool,
);
}
Map<String, dynamic> toJson() {
return {
'userId': userId,
'id': id,
'title': title,
'completed': completed,
};
}
// Here will be more methods after your customization.
// toString();
// copyWith();
@override
List<Object> get props => [userId, id, title, completed];
}
To addEquatable
support you just have to select Yes
when the process of parsing your JSON to Code has started and the extension will take care of setting up the advanced code equality check in your Dart models
If you don’t want install Equatable
package and work with @immutable
classes and values then you can add equality operator ==
with less boilerplate syntax. And customize your class as mutable.
@override
bool operator ==(Object o) =>
o is Todos &&
identical(o.userId, userId) &&
identical(o.id, id) &&
identical(o.title, title) &&
identical(o.completed, completed);
@override
int get hashCode => hashValues(userId, id, title, completed);
You can add toString()
method in your classes to improve the debugging experience.
@override
String toString() {
return 'Todos(userId: $userId, id: $id, title: $title, completed: $completed)';
}
copyWith()
method will make your life easier with @immutable
classes. Highly recommended with imuttable classes.
Todos copyWith({
int userId,
int id,
String title,
bool completed,
}) {
return Todos(
userId: userId ?? this.userId,
id: id ?? this.id,
title: title ?? this.title,
completed: completed ?? this.completed,
);
}
If you’d like to use Code Generation Libraries from Flutter, first of all I suggest you to add dependencies to the pubspec.yaml
file. It also can be done with this extension. You don’t need to worry about it 😉. After that you can convert your JSON
to model classes. Then you need to run the flutter pub run build_runner build
command to generate the missing code of the models, according to Flutter documentation. Fortunately the extension automatically opens a new terminal session and runs that command for you, yey 😄.
Select a valid json. Press Ctrl + shift + P
(linux and mac) or Ctrl + P
(Windows) and search for Convert from Selection
or Convert from Selection to Code Generation supported classes
. Provide a Base class name and location to save.
Copy a valid json. Press Ctrl + shift + P
(linux and mac) or Ctrl + P
(Windows) and search for Convert from Clipboard
or Convert from Clipboard to Code Generation supported classes
. Provide a Base class name and location to save.
Press Ctrl + shift + P
(linux and mac) or Ctrl + P
(Windows) and search for Add Code Generation Libraries to pubspec.yaml
and hit enter.
Using short cuts.
Convert from Clipboard (Shift + Ctrl + Alt + V
)
Convert from Selection (Shift + Ctrl + Alt + S
)
Convert from Clipboard to Code Generation supported classes (Shift + Ctrl + Alt + G
)
Convert from Selection to Code Generation supported classes (Shift + Ctrl + Alt + H
)
Command failed: xclip -selection clipboard -o
Solution:
sudo apt-get install xclip
Happens when linux is missing clipboard packages
❤️ Special thanks to Israel Ibarra for adding equatable support.
❤️ Special thanks to Arnas for suggesting to add Effective Dart: Styles
Feel free to contact me anytime 😊
Author: hiranthaR
Demo: https://marketplace.visualstudio.com/items?itemName=hirantha.json-to-dart
Source Code: https://github.com/hiranthaR/Json-to-Dart-Model
#dart #flutter #mobile-apps