LocalThread<T> constructor

LocalThread<T>()

A Dart equivalent of Java's ThreadLocal, scoped to the current Isolate.

ThreadLocal<T> provides isolate-local storage for values that are only visible within the currently running Isolate. This is useful when you want to maintain separate values across isolates, such as request-scoped data in a concurrent server.

Internally, each isolate maintains its own value store via a LocalThreadStorageBucket, so values set in one isolate will not affect others.

Example

final threadLocal = ThreadLocal<int>();

// In isolate A
threadLocal.set(42);
print(threadLocal.get()); // prints: 42

// In isolate B (separate execution context)
print(threadLocal.get()); // prints: null

This API abstracts away the complexity of isolate-specific data management by allowing you to interact with values as if they were thread-local, even though Dart uses isolates instead of OS threads.

Implementation

LocalThread() : _key = LocalThreadKey.generate();