我正在寻找一种创建唯一字符串以创建文件名的解决方案。我意识到ObjectId()
有一个id.$oid
,它是一个唯一的字符串。但我不确定。这是工作的成功吗?
示例代码:
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;
}
ObjectId().$oid
工作是否正常- 有更好的方法吗
ObjectId
应该是唯一的,因为它是一个12字节的BSON类型的十六进制字符串,包括基于创建时间的随机化。
来源/参考:https://www.knowledgehut.com/blog/web-development/objectid-in-mongodb