deletMomentPicture method

Future<void> deletMomentPicture({
  1. required String momentId,
  2. required String pictureId,
})

Implementation

Future<void> deletMomentPicture({
  required final String momentId,
  required final String pictureId,
}) async {
  try {
    final _momentRef = PeamanReferenceHelper.momentsCol.doc(momentId);

    final _momentSnap = await _momentRef.get();
    if (!_momentSnap.exists)
      throw Future.error('Moment with id $momentId not found');

    final _momentData = _momentSnap.data();
    if (_momentData == null)
      throw Future.error('Moment with id $momentId not found');

    final _moment = PeamanMoment.fromJson(_momentData);

    final _newPictures =
        _moment.pictures.where((element) => element.id != pictureId).toList();

    if (_newPictures.isEmpty) {
      await _momentRef.delete();
    } else {
      await _momentRef.update({
        'pictures': _newPictures.map((e) => e.toJson()).toList(),
      });
    }
    print("Success: Deleting moment picture $pictureId");
  } catch (e) {
    print(e);
    print('Error!!!: Deleting moment picture');
  }
}