update method

  1. @override
Future<RecordModel> update(
  1. String id, {
  2. Map<String, dynamic> body = const {},
  3. Map<String, dynamic> query = const {},
  4. List<MultipartFile> files = const [],
  5. Map<String, String> headers = const {},
  6. String? expand,
  7. String? fields,
})
override

Updates a single record model by its id.

If the current AuthStore.record matches with the updated id, then on success the client AuthStore will be updated with the result model.

Implementation

@override
Future<RecordModel> update(
  String id, {
  Map<String, dynamic> body = const {},
  Map<String, dynamic> query = const {},
  List<http.MultipartFile> files = const [],
  Map<String, String> headers = const {},
  String? expand,
  String? fields,
}) {
  return super
      .update(
    id,
    body: body,
    query: query,
    files: files,
    headers: headers,
    expand: expand,
    fields: fields,
  )
      .then((item) {
    if (client.authStore.record != null &&
            (client.authStore.record as RecordModel).id == item.id &&
            [
              (client.authStore.record as RecordModel).collectionId,
              (client.authStore.record as RecordModel).collectionName,
            ].contains(_collectionIdOrName)
        // merge the response fields with the current auth record
        ) {
      final expand = (client.authStore.record as RecordModel).data["expand"]
              as Map<String, dynamic>? ??
          {};

      final newExpand = item.data["expand"] as Map<String, dynamic>? ?? {};

      final data = <String, dynamic>{}
        ..addAll((client.authStore.record as RecordModel).data)
        ..addAll(item.toJson());

      // for now merge only top-level expand keys
      if (expand.isNotEmpty) {
        expand.addAll(newExpand);
        data["expand"] = expand;
      }

      client.authStore.save(
        client.authStore.token,
        RecordModel.fromJson(data),
      );
    }

    return item;
  });
}