Last active
June 25, 2022 22:41
Star
You must be signed in to star a gist
This file contains 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