Add base model

This commit is contained in:
Sambo Chea 2022-10-28 10:33:56 +07:00
parent 3b2abdc030
commit a472156d22
Signed by: sombochea
GPG Key ID: 3C7CF22A05D95490
3 changed files with 60 additions and 10 deletions

View File

@ -1,16 +1,14 @@
import 'dart:convert';
import 'package:cubetiq/interfaces.dart';
import 'package:cubetiq/model.dart';
void main(List<String> args) {
MyCart.addCart(
Cart(1, 'Apple', 1),
Cart(id: 1, name: 'Apple', qty: 1),
);
MyCart.show();
MyCart.addCart(
Cart(1, 'Apple', 2),
Cart(id: 1, name: 'Apple', qty: 2),
);
MyCart.show();
@ -24,15 +22,24 @@ void main(List<String> args) {
MyCart.show();
}
class Cart implements ToJson {
class Cart extends BaseModel<Cart> {
final int id;
final String name;
final String? name;
double qty;
Cart(this.id, this.name, this.qty);
Cart({this.id = -1, this.name, this.qty = 0});
@override
Map toJson() {
Cart fromMap(Map<String, dynamic> map) {
return Cart(
id: map['id'],
name: map['name'],
qty: map['qty'],
);
}
@override
Map<String, dynamic> toMap() {
return {
'id': id,
'name': name,
@ -71,7 +78,14 @@ class MyCart {
}
static void show() {
var data = carts.map((key, value) => MapEntry(key, jsonEncode(value)));
var data = carts.map((key, value) => MapEntry(key, value));
var json = data.map((key, value) => MapEntry(key, value.toJson()));
var model = json.map((key, value) => MapEntry(key, Cart().fromJson(value)));
print('================ Map =================');
print('$data');
print('================ Json =================');
print('$json');
print('================ Model =================');
print('$model');
}
}

3
lib/model.dart Normal file
View File

@ -0,0 +1,3 @@
library model;
export 'src/model/base.dart';

33
lib/src/model/base.dart Normal file
View File

@ -0,0 +1,33 @@
import 'dart:convert';
abstract class BaseModel<T> {
BaseModel();
Map<String, dynamic> toMap();
T fromMap(Map<String, dynamic> map);
T fromJson(String json) {
return fromMap(decode(json));
}
@override
String toString() {
return toMap().toString();
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is BaseModel && other.toMap() == toMap();
}
@override
int get hashCode => toMap().hashCode;
String toJson() => encode(toMap());
dynamic decode(String json) => jsonDecode(json);
String encode(dynamic json) => jsonEncode(json);
}