IBaseModel<T> class
abstract
background_json_parser
With this package you can easily parse your json and convert your model to json.
Feature:
- Parse json with main thread.
- Parse json with multi thread (Asynchronous).
- Convert model to json with main thread.
- Convert model to json with multi thread (Asynchronous).
Usage:
It has a very easy to use.
- Add this line to pubspec.yaml.
dependencies:
background_json_parser: ^1.0.4
- import package.
import 'package:background_json_parser/background_json_parser.dart';
- Extend your model from IBaseModel and enter your model as generic type to IBaseModel.
class UserModel extends IBaseModel<UserModel> {
UserModel({
this.id,
this.name,
this.username,
this.email,
this.address,
this.phone,
this.website,
this.company,
});
int? id;
String? name;
String? username;
String? email;
Address? address;
String? phone;
String? website;
Company? company;
}
- Implement this methods to your class.
@override
fromJson(Map<String, dynamic> json) => UserModel(
id: json["id"],
name: json["name"],
username: json["username"],
email: json["email"],
address: Address.fromJson(json["address"]),
phone: json["phone"],
website: json["website"],
company: Company.fromJson(json["company"]),
);
@override
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"username": username,
"email": email,
"address": address!.toJson(),
"phone": phone,
"website": website,
"company": company!.toJson(),
};
- Your class will be such as.
class UserModel extends IBaseModel<UserModel> {
UserModel({
this.id,
this.name,
this.username,
this.email,
this.address,
this.phone,
this.website,
this.company,
});
int? id;
String? name;
String? username;
String? email;
Address? address;
String? phone;
String? website;
Company? company;
@override
fromJson(Map<String, dynamic> json) => UserModel(
id: json["id"],
name: json["name"],
username: json["username"],
email: json["email"],
address: Address.fromJson(json["address"]),
phone: json["phone"],
website: json["website"],
company: Company.fromJson(json["company"]),
);
@override
Map<String, dynamic> toJson() => {
"id": id,
"name": name,
"username": username,
"email": email,
"address": address!.toJson(),
"phone": phone,
"website": website,
"company": company!.toJson(),
};
}
- Json parser methods.
- Parse json with main thread.
/// If you want parse json object
UserModel user = UserModel().jsonParser(response.body);
/// If you want parse json array
List<UserModel> userList = UserModel().jsonParser(response.body);
- Parse json with multi thread.
/// If you want parse json object
UserModel user = await UserModel().backgroundJsonParser(response.body);
/// If you want parse json array
List<UserModel> userList = await UserModel().backgroundJsonParser(response.body);
- Convert your model to json.
- Convert your model to json with main thread.
/// If you want convert your Model
String json = userModel.convertToJson();
/// If you want convert your List of Model
String json = userModel().convertToJson(list);
- Convert your model to json with multi thread.
/// If you want convert your Model
String json = await userModel.backgroundConvertToJson();
/// If you want convert your List of Model
String json = await userModel().backgroundConvertToJson(list);
Constructors
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
-
backgroundConvertToJson(
[List< T> ? model]) → Future<String> - You can convert your model to json in background in (with multi thread) such as String json = await userModel.backgroundConvertToJson();
-
backgroundJsonParser(
String jsonBody) → Future - You can parse your json in background (multi thread) with this method such as: UserModel user = await UserModel().backgroundJsonParser(response.body);
-
convertToJson(
[dynamic model]) → String - You can convert your model to json such as String json = userModel.convertToJson();
-
fromJson(
Map< String, dynamic> json) → T -
Override this method and convert map (argument) to your class object such as:
@override
UserModel fromJson(Map<String, dynamic> json) => UserModel(
id: json
"id"
, name: json"name"
, username: json"username"
, email: json"email"
, address: Address.fromJson(json"address"
), phone: json"phone"
, website: json"website"
, company: Company.fromJson(json"company"
), ); -
jsonParser(
String jsonBody) → dynamic - You can parse your json with this method such as: UserModel user = UserModel().jsonParser(response.body);
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toJson(
) → Map< String, dynamic> - Override this method and Convert your class object to Map such as: @override Map<String, dynamic> toJson() => { "id": id, "name": name, "username": username, "email": email, "address": address!.toJson(), "phone": phone, "website": website, "company": company!.toJson(), };
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited