LocalThread<T> class
final
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.
- Implementers
- Annotations
-
- @Generic(LocalThread)
Constructors
- LocalThread()
-
A Dart equivalent of Java's
ThreadLocal
, scoped to the current Isolate.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
get(
) → T? - Returns the value associated with this LocalThread in the current Isolate.
-
initialValue(
) → T? - Returns the initial value for this LocalThread.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
remove(
) → void - Removes the value associated with this LocalThread in the current Isolate.
-
set(
T value) → void - Sets a value for this LocalThread in the current Isolate.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited