Comments (11)
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.
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.
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.
@eugvb Please report if the comment above solved your issue.
from flutterfire.
Maybe it is not perfect way, but I persist Document Id inside it. So I need generate id before calling add()
method.
from flutterfire.
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.
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.
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.
It is solution, thank you
from flutterfire.
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.
They are unique: https://stackoverflow.com/questions/46618719/firestore-are-ids-unique-in-the-collection-or-globally
from flutterfire.
Related Issues (20)
- [firebase_messaging]: <Web notification not working on Production> HOT 3
- [FIREBASE MESSAGING]: java.lang.NullPointerException: Firebase Messaging component is not present, Stacktrace: HOT 6
- cloud_firestore(windows): empty document crash
- cloud_firestore: TypeError: Cannot read properties of undefined (reading 'getFirestore') HOT 9
- [firebase_analytics]: in_app_purchase event is not being auto reported for iOS HOT 2
- [CLOUD_FIRESTORE]: Invalid Query Error: Conflict Between array_contains_any and not_in Filters HOT 2
- [flutterfire_cli]: CLI creates plugin references wrongly in Android HOT 2
- [firebase_crashlytics]: Flutter App Crashes on Newer Devices After Submission to Google Play HOT 1
- [firebase_core]: <issue in android> HOT 1
- signInWithRedirect is of void type, but is stated as returning UserCredentials in the docs HOT 1
- FirebaseCoreHostApi.initializeCore HOT 2
- Domain=FIRAuthErrorDomain Code=17999 with firebase auth HOT 2
- [FirebaseAuth]: Unhandled Exception: type 'List<Object?>' is not a subtype of type 'PigeonUserInfo' in type cast HOT 1
- firebase_auth: Error: Type 'IdTokenResult' not found. HOT 3
- [firebase_crashlytics]: java.io.IOException: Breakpad symbol generation failed (exit=1) by GitHub Actions HOT 5
- [firebase_hosting] Wasm is not used in Flutter Web App HOT 2
- [firebase_messaging]: background proccesing messaging not working 17.5.1 ios HOT 1
- [cloud_firestore]: ListenSource.cache seems to be ignored for document snapshots HOT 1
- [firebase_core]: <runTransaction throws an exception on Windows.>
- [firebase_database]: Issue on web debug mode when listening to multiple onValue streams on the same path HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flutterfire.