如何检查控制器是否安装了 Flutter GetX?

use*_*444 10 dart flutter flutter-getx

我有以下代码:

Get.put(DbController(HabitDao(AppDb())));
Run Code Online (Sandbox Code Playgroud)

当我热重新加载我的应用程序时,出现以下错误:

"WARNING (moor): It looks like you've created the database class AppDb multiple times. When these two databases use the same QueryExecutor, race conditions will occur and might corrupt the database."
Run Code Online (Sandbox Code Playgroud)

DbController我需要检查在创建新实例之前是否放置了 a AppDb。我在使用 Provider 包时从未遇到过这个问题。

解决这个问题的最佳实践是什么?

Ata*_*lut 45

您可以使用 进行检查Get.isRegistered

对于您的代码:

bool test = Get.isRegistered<DbController>(tag: 'TagInPut');
Run Code Online (Sandbox Code Playgroud)

如果您没有使用标签,则可以将其删除。

bool test = Get.isRegistered<DbController>();
Run Code Online (Sandbox Code Playgroud)