Flutter firestore - Check if document ID already exists

前端 未结 4 1716
终归单人心
终归单人心 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:28

    Use the exists method on the snapshot:

    final snapShot = await Firestore.instance.collection('posts').document("docID").get();
    
       if (snapShot.exists){
            //it exists
       }
       else{
            //not exists 
       }
    

提交回复
热议问题