Dart Flutter Tips

Why I Write Dollar Signs in Code

Typically use dollar signs in my code for three main purposes...
Plague Fox 1 min read
Why I Write Dollar Signs in Code
Photo by Alexander Grey / Unsplash

As a Separator and for Hierarchy

For example, it helps in distinguishing and organizing code in a clear hierarchy:

YourScreen
YourScreen$Form
_YourScreen$Form$Buttons

This notation is both convenient and understandable, allowing for quick selection of a name part with a double mouse click.

Additionally, for unions and sealed classes:

sealed class Base {}
base mixin M on Base {}
final class Base$A = Base with M; 
final class Base$B = Base with M; 
final class Base$C = Base with M; 
final class Base$D = Base with M; 

To Indicate Auxiliary or Private Use

When used at the beginning of a name, it signifies that the variable is auxiliary and typically not meant for public use. Moreover, unless marked private, it tends to be less prominent in IDE tooltips:

$someVar
_$someVar

For instance, I frequently use such variables for caching:

T? _$a;
T get a => _$a ??= _init();

Future<T>? _$fn;
Future<T> fn() => _$fn ??= Future(() {});

AsyncCache<T> _$cache;

This approach is also handy for using functions with conditional imports, especially when needing to differentiate imports by VM and JS environments.

For Code Generation and When Using an Analyzer

The use of dollar signs is highly situational here, particularly beneficial for code generation and when analyzing source code with an analyzer. This method allows for a more nuanced and flexible approach to code structure and organization, adapting to various programming needs.

Share
Comments
More from Plague Fox
Form State Management
Dart Flutter Article Tips

Form State Management

Flutter's SDK already has everything you need to manage complex forms — no packages required. Listenable.merge turns any combination of ValueNotifier, TextEditingController, FocusNode, and ChangeNotifier into a single reactive form controller.
Plague Fox 4 min read
Safe Resource Cleanup with Closure Chains
Dart Flutter Article

Safe Resource Cleanup with Closure Chains

Dart has no defer, no RAII, no scope guards. When multi-step async initialization fails midway, you need to unwind only what was set up — in reverse order, crash-safe. Here's a 6-line closure chain pattern that gives you transactional init, safe teardown, and cancellation support for free.
Plague Fox 5 min read

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.