1

I am finding a solution to create a unique string to create a file name. I realized that the ObjectId() has an id.$oid that is a unique string. But I am not sure. Is it work success full with this?

example code:

List<FileObject> generateFileName(List<FileObject> files) {
  return files.map((e) => e..updateName(ObjectId().$oid)).toList();
}

class FileObject {
  FileObject({this.name, this.path, this.url, this.type});

  String? name;
  String? path;
  String? url;
  String? type;

  void updateName(String? name) => this.name = name;
}
  • Is the ObjectId().$oid working perfectly?
  • Is there a better way?

1 Answers1

1

An ObjectId should be unique as it is a 12-byte BSON type hexadecimal string that includes randomization based on the time its created.

Source/reference: https://www.knowledgehut.com/blog/web-development/objectid-in-mongodb

Matthew Trent
  • 2,611
  • 1
  • 17
  • 30
  • Thank you for the useful link. The `ObjectId` is unique then this support creating the file name. – Sittiphan Sittisak Nov 02 '22 at 08:54
  • @SittiphanSittisak Awesome! I’m glad it worked! If it’s the answer you were looking for, please give it a check for users in the future :) – Matthew Trent Nov 02 '22 at 09:00
  • I am thinking about my second question. I think the method to create a unique string has many solutions as shown in many questions in StackOverflow. Some answers use the package, and some answers create a custom code. I think these all solutions just are the option. No one is wrong. I will use `ObjectId()` from `mongo_dart` just because I am using this package and I don't want to add a package that gives the same result as `ObjectId()`. If you disagree with me. You can create new answers to my 2 questions and I will check them. – Sittiphan Sittisak Nov 02 '22 at 09:06