final id = ObjectId(); // That's all 🔥😮!print(id); // it's working! => 5f52c805df41c9df948e6135
Documentation
Constructors
ObjectId()
Creates an ObjectId instance based on current timestamp, process unique and counter.
final id = ObjectId();
print(id.hexString); // => 5f52c805df41c9df948e6135
ObjectId.fromHexString(String hexString)
Creates ObjectId from a 24-character hex string.
// Useful for mapping hex strings from APIs or MongoDBfinal id = ObjectId.fromHexString('5f52c805df41c9df948e6135');
print(id == ObjectId.fromHexString(id.hexString)); // => true
ObjectId.fromBytes(List<int> bytes)
Creates ObjectId from a 12-byte array.
// Perfect for storing ObjectId in binary formatfinal id = ObjectId.fromBytes([95, 82, 205, 121, 180, 195, 28, 88, 32, 47, 183, 78]);
print(id.hexString); // => 5f52cd79b4c31c58202fb74e// Retrieve bytes with the bytes propertyfinal id2 = ObjectId.fromBytes(id.bytes);
print(id == id2); // => true
ObjectId.fromValues(int millisecondsSinceEpoch, int processUnique, int counter)