Dart Flutter Tips

Providing data using zones

Use the 'zoneValues' argument to 'runZoned()' to store values in the newly created zone.
Plague Fox 1 min read
Providing data using zones
Photo by ClΓ©ment HΓ©lardot / Unsplash

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]);
}

Read more

Share
Comments
More from Plague Fox

Plague Fox

Engineer by day, fox by night. Talks about Flutter & Dart.

Great! You’ve successfully signed up.

Welcome back! You've successfully signed in.

You've successfully subscribed to Plague Fox.

Success! Check your email for magic link to sign-in.

Success! Your billing info has been updated.

Your billing was not updated.