saveObjects<T extends VasatModel<T>> method
Future<bool>
saveObjects<
T extends VasatModel<T>>( - List<T> objects
)
override
Implementation
@override
Future<bool> saveObjects<T extends VasatModel<T>>(List<T> objects) {
return SharedPreferences.getInstance().then((prefs) {
// Get first object to determine the vasat object name
String objName = objects.first.getObjectName();
// Get already stored object list as a Map<String,Object> where key is the id
var objs = prefs.getString(objName);
Map<String,dynamic> objsMap = {};
if (objs != null) objsMap = jsonDecode(objs);
// generate new list of Map objects
Map<String,dynamic> newObjsMap = {};
objects.forEach((element) {
String uid = (element.id == null) ? generateUID(T) : element.id!;
if (element.id == null) element.id = uid;
newObjsMap[uid] = element.toJson();
} );
// Concatenate both maps replacing values on old Map with new Map if keys/id are already present.
objsMap.addAll(newObjsMap);
return prefs.setString( objName , jsonEncode(objsMap));
});
}