Skip to content

Instantly share code, notes, and snippets.

@M97Chahboun
Last active June 25, 2022 22:41
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save M97Chahboun/d567ab77e4b1eaf9647d09c807985ea7 to your computer and use it in GitHub Desktop.
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