Coder Social home page Coder Social logo

Comments (11)

sultanmyrza avatar sultanmyrza commented on August 26, 2024 4

I know its late but here is what I did

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

from flutterfire.

Gintasz avatar Gintasz commented on August 26, 2024 1

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: ""

from flutterfire.

cmkweber avatar cmkweber commented on August 26, 2024

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

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

from flutterfire.

BondarenkoStas avatar BondarenkoStas commented on August 26, 2024

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

from flutterfire.

eugvb avatar eugvb commented on August 26, 2024

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

from flutterfire.

tantzygames avatar tantzygames commented on August 26, 2024

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.

from flutterfire.

eugvb avatar eugvb commented on August 26, 2024

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.

from flutterfire.

cmkweber avatar cmkweber commented on August 26, 2024

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.

from flutterfire.

eugvb avatar eugvb commented on August 26, 2024

It is solution, thank you

from flutterfire.

Limitless-Kode avatar Limitless-Kode commented on August 26, 2024

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.

from flutterfire.

cmkweber avatar cmkweber commented on August 26, 2024

They are unique: https://stackoverflow.com/questions/46618719/firestore-are-ids-unique-in-the-collection-or-globally

from flutterfire.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.