JsonSerializable class abstract

This class provides a Json Serializable mechanism, you can use it with any class that you want a basic behaviour to generate Map or Json and desired class from Map or Json.

How to use it (keep sure your constructor uses named parameters):

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

 User({required this.id, required this.name, required this.age});
}

Then you should be able to use the following methods: toMap, toJson, fromMap, fromJson

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

Constructors

JsonSerializable()

Properties

hashCode int
The hash code for this object.
no setterinherited
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
toJson({List<String> excludes = const []}) String
toMap({List<String> excludes = const []}) Map<String, dynamic>
toString() String
A string representation of this object.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited

Static Methods

fromJson<B extends Object>(String source, {List<String> excludes = const []}) → B
fromMap<B extends Object>(Map<String, dynamic> source, {List<String> excludes = const []}) → B