Flutter firestore - Check if document ID already exists

前端 未结 4 1723
终归单人心
终归单人心 2020-12-02 01:48

I want to add data into the firestore database if the document ID doesn\'t already exists. What I\'ve tried so far:

// varuId == the ID that is set to the do         


        
4条回答
  •  盖世英雄少女心
    2020-12-02 02:35

    You can get() the document and use the exists property on the snapshot to check whether the document exists or not.

    An example:

    final snapShot = await Firestore.instance
      .collection('posts')
      .document(docId)
      .get()
    
    if (snapShot == null || !snapShot.exists) {
      // Document with id == docId doesn't exist.
    }
    

提交回复
热议问题