If you ever wanted to use a static variable but couldn’t because multiple concurrently running computations interfered with each other, consider using a zone-local value. You might add a zone-local value to help with debugging. Another use case is dealing with an HTTP request: you could have the user ID and its authorization token in zone-local values.
Use the zoneValues
argument to runZoned()
to store values in the newly created zone:
import 'dart:async';
void main() => runZoned<void>(
() => functionA(),
zoneValues: <Object?, Object?>{
#my.zone.key: 'Data for current stack trace',
},
);
Future<void> functionA() async {
functionB();
}
Future<void> functionB() async {
print(Zone.current[#my.zone.key]);
}