Skip to content

[cloud_firestore] Need id generator like createId() function in AngularFire #49

Description

@eugvb

In some cases I need generate id before store document in firestore.
In AngularFire there is function createId()
In Flutter I can import 'package:cloud_firestore/src/utils/auto_id_generator.dart' (since 0.12.8, push_id_generator before)
and then use AutoIdGenerator.autoId()
But it's not true way, because I should import implementation files from another package.

Activity

  1. cmkweber commented on Sep 9, 2019

    @cmkweber

    CollectionReference has an add method which returns an auto-generated ID:

    https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/CollectionReference/add.html

  2. BondarenkoStas commented on Sep 11, 2019

    @BondarenkoStas

    @eugvb Please report if the comment above solved your issue.

  3. eugvb commented on Sep 12, 2019

    @eugvb
    Author

    Maybe it is not perfect way, but I persist Document Id inside it. So I need generate id before calling add() method.

  4. tantzygames commented on Sep 13, 2019

    @tantzygames

    I use:
    String id = reference.document().documentID;

    Then apply the id within the document and any other documents, then save.

    There is an issue with both this and add() that is only occurring recently. iOS is creating a different format of id than Android, where they used to be the same. So you may be better off creating your own id system...

    Edit: I notice this was new to 0.12.7 but is now consistent across iOS and Android in 0.12.8. Unfortunately the new format of id's are no longer chronological so I'm using the old PushIdGenerator for id's that I prefer to be chronological.

  5. eugvb commented on Oct 8, 2019

    @eugvb
    Author

    Ok, storing Document Id in itself not good practice. So in this case no need for id generation.

    But what about transaction or batch writes?
    For example, I want to add document to collection and then store it Id in other document

    Firestore.instance.runTransaction((tx) async {
      String docId = 'docId';//It should be generated
      await tx.set(Firestore.instance.collection('collection').document(docId), {'some':'data'});
      //There i need docId to store it in other Document
      await tx.set(Firestore.instance.collection('collection2').document('DocumentId2'), {'docId':docId});
    });
    

    Transaction doesn't have add method, so I need use set with generated id.
    With Batch write situation is similar.

  6. cmkweber commented on Oct 8, 2019

    @cmkweber

    Use document without passing a path:
    https://pub.dev/documentation/cloud_firestore/latest/cloud_firestore/CollectionReference/document.html
    This generates a unique id without setting it within database like add does.

  7. eugvb commented on Oct 8, 2019

    @eugvb
    Author

    It is solution, thank you

  8. sultanmyrza commented on May 2, 2020

    @sultanmyrza

    I know its late but here is what I did

    String randomId() {
        var _randomId = Firestore.instance.collection('').document().documentID;
        print(randomId());
        return _randomId;
    }
    
  9. rockclaver commented on May 29, 2020

    @rockclaver

    I know its late but here is what I did

    String randomId() {
        var _randomId = Firestore.instance.collection('').document().documentID;
        print(randomId());
        return _randomId;
    }
    

    Can this produce a unique ID? I am thinking it would somehow create an ID that already exists.

  10. gintasz commented on Jul 20, 2020

    @gintasz

    I know its late but here is what I did

    String randomId() {
        var _randomId = Firestore.instance.collection('').document().documentID;
        print(randomId());
        return _randomId;
    }
    

    Doesn't work on web at the present moment (cloud_firestore: 0.13.7), getting this error:
    Function Firestore.collection() requires its first argument to be of type non-empty string, but it was: ""

  11. locked and limited conversation to collaborators on Aug 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions