如何检查控制器是否已放入 Flutter GetX?

How to check if a controller has put with Flutter GetX?

我有以下代码:

Get.put(DbController(HabitDao(AppDb())));

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

"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."

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

解决此问题的最佳做法是什么?

您可以通过Get.isRegistered查看。

对于您的代码:

bool test = Get.isRegistered<DbController>(tag: 'TagInPut');

如果你没有使用标签,你可以删除它。

bool test = Get.isRegistered<DbController>();