1
votes

The 'utenti' (users) collection contains a document for each user which in turn contains the 'salvataggi' (saves) collection to save objects within the app.

Each document inside 'salvataggi' (saves) has an automatically generated id containing a series of data (String to be precise).

Documents within 'salvataggi' (saves) can be added by saving objects created from two other collections always in FIrestore.

When, through a button, I want to delete an object from the 'salvataggi' (saves) collection, a random document is deleted and not the one corresponding to the object.

Screenshot of Firestore

Object 1

final CollectionReference _usersRef =
          FirebaseFirestore.instance.collection('utenti');

      final User _user = FirebaseAuth.instance.currentUser;

      //Add
      Future _addToSaved() { 
        return _usersRef.doc(_user.uid).collection('salvataggi').doc().set({
          'fonte': elenco.fonte,
          'title': elenco.title,
          'url': elenco.urlAvv,
          'imageEv': elenco.urlAvv,
          'p1': elenco.iconaProspettiva1,
          'p1url': elenco.urlProspettiva1,
        });
      }
       
      //Remove
      Future _removeFromSaved() async { 
        CollectionReference userSaved =
            _usersRef.doc(_user.uid).collection('salvataggi');

        QuerySnapshot querySnap = await userSaved.get();

        querySnap.docs[0].reference.delete();
      }

Object 2

final CollectionReference _usersRef =
      FirebaseFirestore.instance.collection('utenti');

  final User _user = FirebaseAuth.instance.currentUser;

  //Add
  Future _addToSaved() {
    return _usersRef.doc(_user.uid).collection('salvataggi').doc().set({
      'fonte': widget.single.fonte,
      'title': widget.single.title,
      'url': widget.single.urlAvv,
      'imageEv': widget.single.imageEvAvv,
      'lastupdate': widget.single.dataAggiornamento,
      'p1': widget.single.iconaProspettiva1,
      'p1url': widget.single.urlProspettiva1,
      
    });
  }
  
  //Remove
  Future _removeFromSaved() async {
    CollectionReference userSaved =
        _usersRef.doc(_user.uid).collection('salvataggi');

    QuerySnapshot querySnap = await userSaved.get();

    querySnap.docs[0].reference.delete();
  }

What am I doing wrong? Why does this happen?

2
You are always deleting the docs[0] so the document with the alphabetically smallest document idahmetakil

2 Answers

1
votes

When the user saves a document try saving the id of that document with it so whenever the user unsaved the document. You can pass the id of that unsaved document to firestore.

It will look something like this

  Future _removeFromSaved(String docID) async {
    CollectionReference userSaved =
        _usersRef.doc(_user.uid).collection('salvataggi');

    await userSaved.doc(docID).delete()
  }

--UPDATE--

You can save document id by calling the then method after adding the document to firestore and then updating it

  Future _addToSaved()async {
    await _usersRef.doc(_user.uid).collection('salvataggi').add({
      'fonte': widget.single.fonte,
      'title': widget.single.title,
      'url': widget.single.urlAvv,
      'imageEv': widget.single.imageEvAvv,
      'lastupdate': widget.single.dataAggiornamento,
      'p1': widget.single.iconaProspettiva1,
      'p1url': widget.single.urlProspettiva1,
      
    }).then(docRef=>await _usersRef.doc(_user.uid).collection('salvataggi').doc(docRef.id).update({'id':docRef.id}));
  }
0
votes
  querySnap.documents.first.reference.delete();

Use this instead of querySnap.docs[0].reference.delete();