What does the dollar sign mean when put before a class call in Flutter?

2.2k views Asked by At

I'm a beginner at Flutter/Dart and I keep seeing dollar signs appearing before calls to classes or methods, an example is the following (found in Floor package documentation):

 final database = await $FloorAppDatabase.databaseBuilder('app_database.db').build();

I've searched a lot and the only meaning of dollar signs in Dart that I could find is for string interpolation, but this doesn't seem the case.

1

There are 1 answers

9
Dai On BEST ANSWER

It's not a Flutter or Dart convention - at least not an official one: the official Dart naming conventions document (as of October 2020) makes no mention of using $ in identifier names.

However, I do know that other programming languages' ecosystems do use a dollar-sign (Sigil) and I think that habit was inherited by the authors of the floor database that you linked to. More specifically, in Java it's commonplace to use a $ prefix for type-names generated by tooling rather than being hand-written (such as ORM entity types, for example), and people using RxJS observables in JavaScript will use a $ as a variable-name suffix.

As $FloorDatabase is a type-name, not a variable or member-name, I'm going to go with the assumption it's a habit picked-up from Java:

Java: The $ character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.

So in this case, a clue is in the documentation you linked to:

Use the generated code. For obtaining an instance of the database, use the generated $FloorAppDatabase class, which allows access to a database builder

So that's my conclusion: it's not an official naming convention in Dart/Flutter, but it is in Java, and it seems like the authors of floor carried it over from Java.

(I personally wish they didn't as it isn't a particularly useful indicator - what does it matter to a consuming application if a type was created a tool instead of hand-written?)