Flutter Firestore add new document with Custom ID

前端 未结 2 925
太阳男子
太阳男子 2021-02-12 20:40

How to add new document with custom id using Dart and Flutter?

PS: I can add new document to collection but its id sets randomly, using this code

2条回答
  •  死守一世寂寞
    2021-02-12 21:06

    You can use set() function instead of add().

    Here's full code:

    final CollectionReference postsRef = Firestore.instance.collection('/posts');
    
    var postID = 1;
    
    Post post = new Post(postID, "title", "content");
    Map postData = post.toJson();
    await postsRef.doc(postID).set(postData);
    

    I hope that help anyone.

提交回复
热议问题