Log Example
SmartCrew Admin - Log Example
Example Codes
if we consider a model like this:
class Example {
final String id;
final String title;
final String description;
final String? imageUrl;
final DateTime createdDate;
}these two methods should be implemented in the model with their fields to detect the changes:
List<String> getChangeList(Map<String, dynamic> json) {
List<String> actions = [];
this.toJson().forEach((key, value) {
if (key == 'imageUrl') return; // Skip image URL changes like in other classes
if (value != json[key]) {
actions.addNullSafety(_createLog(value, json[key], displayNames()[key] ?? key));
}
});
return actions;
}
String? _createLog(dynamic oldValue, dynamic newValue, String key) {
if (oldValue is int) {
return LogUtils.changedFieldLog(DateTime.fromMillisecondsSinceEpoch(oldValue).toString(),
DateTime.fromMillisecondsSinceEpoch(newValue).toString(), capitalizeWords(key), 'Announcement: $title');
} else if (oldValue is String) {
return LogUtils.changedFieldLog(oldValue, newValue, capitalizeWords(key), 'Announcement: $title');
}
return null;
}