json_serializable_generic

Last updated:

0 purchases

json_serializable_generic Image
json_serializable_generic Images
Add to Cart

Description:

json serializable generic

json_serializable #

This package provides a Json Serializable mechanism.
You can add this behavior to any class you like.
Getting Started: #

class User with JsonSerializable {
final int id;
final String name;
final int age;

User({required this.id, required this.name, required this.age});
}
copied to clipboard
At first you need to create a new class or use an existing, then
add the mixin JsonSerializable like the code above.
Make sure thar your constructor uses named parameters.
After your added the JsonSerializable to an Class you can will see
the following new methods

toMap - use it if you want to convert your current instance to a Map structure
toJson - use it if you want to convert your current instance to a json encoded data
fromMap - use it if you want to convert a Map structure into a Class. Make sure that your constructor uses named parameters
fromJson - use it if you want to convert a json encoded data into a class. Make sure that your constructor uses named parameters

To import json_serializable:
import 'package:json_serializable/json_serializable.dart';
copied to clipboard
Full example: #

import 'package:json_serializable/json_serializable.dart';

import 'models/user_model.dart';

void exampleWithMap() {
var commonUser = User(id: 1, name: 'Dash', age: 10);

print(commonUser.toMap());

var parsedUser = JsonSerializable.fromMap<User>(commonUser.toMap());

print(parsedUser.toMap());
}

void exampleWithJson() {
var commonUser = User(id: 1, name: 'Dash', age: 10);

print(commonUser.toJson());

var parsedUser = JsonSerializable.fromJson<User>(commonUser.toJson());

print(parsedUser.toJson());
}

void main() {
try {
exampleWithMap();
exampleWithJson();
} catch (e) {
print(e);
}
}

copied to clipboard
Extras #

Now you can use a optional parameter on toMap and fromMap methods, if your want to exclude some variable when convert a class to Map send a List<String>, and the same idea can be used when you want to create a instance of an class from Map data but not want some fields because them not used on your constructor.

To use excludes key whe using toMap method:
var user = User(id: 1, name: 'Dash', age: 10);

print(user.toMap(excludes: ['id']));

//{name: Dash, age: 10}
copied to clipboard
To use excludes key when trying parse Map data into a instance of an class:
var mapUser = {
"id": 2,
"name": "foo",
"lastName": "bar",
"age": 11,
};

var user =
JsonSerializable.fromMap<User>(mapUser, excludes:['lastName']);

print(user.toMap());

//{id: 2, name: foo, age: 11}
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.