save method Null safety
Saves the object to the cloud.
Can also specify whether to fetchWhenSave
,
or only saving the object when it matches the query
.
Implementation
Future<LCObject> save(
{bool fetchWhenSave = false, LCQuery<LCObject>? query}) async {
// 检测循环依赖
if (_LCBatch.hasCircleReference(this)) {
throw new ArgumentError('Found a circle dependency when save.');
}
// 保存对象依赖
Queue<_LCBatch> batches = _LCBatch.batchObjects([this], false);
if (batches.length > 0) {
await _saveBatches(batches);
}
// 保存对象本身
String path = objectId == null
? 'classes/$className'
: 'classes/$className/$objectId';
Map<String, dynamic> queryParams = {};
if (fetchWhenSave) {
queryParams['fetchWhenSave'] = true;
}
if (query != null) {
queryParams['where'] = query._buildWhere();
}
Map<String, dynamic> response = objectId == null
? await LeanCloud._httpClient.post(path,
data: _LCEncoder.encode(_operationMap), queryParams: queryParams)
: await LeanCloud._httpClient.put(path,
data: _LCEncoder.encode(_operationMap), queryParams: queryParams);
_LCObjectData data = _LCObjectData.decode(response);
_merge(data);
return this;
}