Last active
June 25, 2022 22:41
-
-
Save M97Chahboun/d567ab77e4b1eaf9647d09c807985ea7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:mvc_rocket/mvc_rocket.dart'; | |
const String postUserIdKey = "userId"; | |
const String postIdKey = "id"; | |
const String postTitleKey = "title"; | |
const String postBodyKey = "body"; | |
class Post extends RocketModel<Post> { | |
int? userId; | |
int? id; | |
String? title; | |
String? body; | |
// disable logs debugging | |
@override | |
bool get enableDebug => false; | |
Post({ | |
this.userId, | |
this.id, | |
this.title, | |
this.body, | |
}); | |
@override | |
void fromJson(covariant Map<String, dynamic> json, {bool isSub = false}) { | |
userId = json[postUserIdKey] ?? userId; | |
id = json[postIdKey] ?? id; | |
title = json[postTitleKey] ?? title; | |
body = json[postBodyKey] ?? body; | |
super.fromJson(json, isSub: isSub); | |
} | |
// Handle models error | |
@override | |
void setException(RocketException? _response) { | |
//SendErrorToCrashlytics.sendError(_response!); | |
super.setException(_response); | |
} | |
void updateFields({ | |
int? userIdField, | |
int? idField, | |
String? titleField, | |
String? bodyField, | |
}) { | |
userId = userIdField ?? userId; | |
id = idField ?? id; | |
title = titleField ?? title; | |
body = bodyField ?? body; | |
rebuildWidget(); | |
} | |
@override | |
Map<String, dynamic> toJson() { | |
final Map<String, dynamic> data = {}; | |
data[postUserIdKey] = userId; | |
data[postIdKey] = id; | |
data[postTitleKey] = title; | |
data[postBodyKey] = body; | |
return data; | |
} | |
// Define if we have multi model | |
@override | |
get instance => Post(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment