搜索插件、代码仓与 APP 分类

插件详情v4.0.3

objectid

适用于 Dart 语言的快速、跨平台 ObjectId 实现。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
3万
喜欢数
38
Pub 分数
160

安装插件

flutter pub add objectid

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个高性能、跨平台的 Dart ObjectId 实现,专为在多个平台上高效生成和处理唯一标识符而设计。该实现以速度和兼容性为目标,支持 Android、iOS、Web、macOS、Windows 和 Linux。适用于需要安全且全局唯一的 ID 的应用程序——例如数据库或分布式系统——此包确保了一致性和可靠性,同时不牺牲性能。在后端服务或实时应用中,当 ID 唯一性和生成速度至关重要时,它尤其有用。

适合场景

  • 数据库文档ID生成
  • 分布式系统标识符创建
  • 实时应用数据同步
  • 安全会话令牌生成
  • 跨平台应用数据跟踪

主要特点

  • 极速 ID 生成
  • 跨平台兼容性
  • 线程安全的实现
  • 紧凑的12字节ID格式
  • 支持时间戳、机器、进程和计数器组件
  • 与 Flutter 应用轻松集成

适合对象

  • 开发可扩展的后端服务的开发者
  • 跨平台需要唯一标识符的应用
  • 使用 MongoDB 或类似数据库的项目
  • 高吞吐量实时应用
  • 注重性能和可移植性的团队

使用建议

  • 不适合用于加密密钥
  • 在多线程环境中需要谨慎处理
  • 不提供 UUID 兼容性
  • 仅限 ObjectId 特定用例
  • 无内置验证工具

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

ObjectId

Blazing fast, cross-platform ObjectId implementation for the dart language!

Dart tests codecov pub package License: MIT style: lints/recommended

Why this package?

Getting started

  1. Depend on it.
dependencies:
  objectid: 4.0.3
  1. Play with it!
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 MongoDB
final 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 format
final id = ObjectId.fromBytes([95, 82, 205, 121, 180, 195, 28, 88, 32, 47, 183, 78]);
print(id.hexString); // => 5f52cd79b4c31c58202fb74e

// Retrieve bytes with the bytes property
final id2 = ObjectId.fromBytes(id.bytes);
print(id == id2); // => true
ObjectId.fromValues(int millisecondsSinceEpoch, int processUnique, int counter)

Creates an ObjectId from provided integer values.

// Examples of different ObjectId patterns
final zeroed = ObjectId.fromValues(0, 0, 0);                    // 000000000000000000000000
final withTimestamp = ObjectId.fromValues(0x3e7fffffc18, 0, 0); // ffffffff0000000000000000
final withProcessUnique = ObjectId.fromValues(0, 0xffffffffff, 0); // 00000000ffffffffff000000
final withCounter = ObjectId.fromValues(0, 0, 0xffffff);        // 000000000000000000ffffff
final filled = ObjectId.fromValues(0x3e7fffffc18, 0xffffffffff, 0xffffff); // ffffffffffffffffffffffff
ObjectId.fromTimestamp(DateTime timestamp)

Creates ObjectId from provided timestamp with other segments zeroed out.

// Useful for ObjectId comparisons or sorting
final id = ObjectId.fromTimestamp(DateTime.now());
print(id.hexString); // => 5f52d05e0000000000000000
Properties and Methods
String hexString

Returns a 24-character hex string representation of the ObjectId (cached for performance).

DateTime timestamp

Returns the generation time accurate to the second (cached for performance).

Uint8List bytes

Returns the ObjectId's raw byte representation.

int hashCode

Uses MurmurHash2 algorithm for fast hashing (cached for performance).

operator ==

Compares ObjectIds based on type and byte equality.

static bool isValid(String hexString)

Checks if a string is a valid ObjectId hex string.

print(ObjectId.isValid('5f52c805df41c9df948e6135')); // => true
print(ObjectId.isValid('invalid')); // => false

All implementation details conform to the BSON ObjectId specification.

Benchmark

Benchmark hardware/software: UNIT: MacBook Pro (M4 Pro, 2024) CPU: Apple M4 Pro RAM: 48 GB OS: macOS 26.3.1 Dart SDK version: 3.11.4

Constructors:
ObjectId() → (RunTime): 0.28017946395452636 us.
ObjectId.fromHexString() → (RunTime): 0.1964833532549569 us.
ObjectId.fromBytes() → (RunTime): 0.11237176106442358 us.
ObjectId.fromValues() → (RunTime): 0.02774876977800984 us.
ObjectId.fromTimestamp() → (RunTime): 0.04264624207353758 us.
Properties:
ObjectId.hexString → (RunTime): 0.03615293445770598 us.
ObjectId.timestamp → (RunTime): 0.03690251970477984 us.
ObjectId.hashCode → (RunTime): 0.02660910959699762 us.
Operators:
ObjectId == ObjectId → (RunTime): 0.08761381094863427 us.

Benchmark is available in the example app.

所属分类

源码仓库

使用此插件的项目