我是 Flutter/Dart 的初学者,我一直看到在调用类或方法之前出现美元符号,示例如下(在 Floor 包文档中找到):
final database = await $FloorAppDatabase.databaseBuilder('app_database.db').build();
我已经搜索了很多,我能找到的 Dart 中美元符号的唯一含义是用于字符串插值,但情况似乎并非如此。
最佳答案
这不是 Flutter 或 Dart 约定——至少不是官方约定:the official Dart naming conventions document (截至 2020 年 10 月)未提及使用 $
在标识符名称中。
但是,我确实知道其他编程语言的生态系统确实使用美元符号( Sigil ),而且我认为这种习惯是由 floor
的作者继承的。您链接到的数据库。更具体地说,in Java it's commonplace to use a $
prefix for type-names generated by tooling而不是手写(例如 ORM 实体类型),并且人们在 JavaScript 中使用 RxJS observables 将使用 a $
as a variable-name suffix .
如 $FloorDatabase
是类型名称,而不是变量或成员名称,我将假设它是从 Java 中习得的习惯:
Java: The
$
character should be used only in mechanically generated source code or, rarely, to access pre-existing names on legacy systems.
所以在这种情况下,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
所以这就是我的结论:它不是 Dart/Flutter 中的官方命名约定,而是 Java 中的命名约定,而且似乎是
floor
的作者。它是从 Java 继承过来的。(我个人希望他们没有,因为它不是一个特别有用的指标——如果一个类型是创建一个工具而不是手写的,这对消费应用程序有什么影响?)
关于flutter - 这是什么时候的 flutter 了一流的调用前把美元符号是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64190614/