Using Obx, got this error: [Get] the improper use of a GetX has been detected

Using Obx, got this error: [Get] the improper use of a GetX has been detected

请帮帮我。我做错了什么?我收到错误: [Get] the improper use of a GetX has been detected.

这是代码:

class MealRecipesItem extends StatefulWidget {
  const MealRecipesItem({
    Key? key,
    @required this.gender,
    this.item,
  }) : super(key: key);

  final int? gender;
  final Data? item;

  @override
  _MealRecipesItemState createState() => _MealRecipesItemState(item?.id);
}

class _MealRecipesItemState extends State<MealRecipesItem> {
  final itemId;

  _MealRecipesItemState(this.itemId) {
    Get.put(RecipeDetailController(), tag: itemId);
  }

  @override
  Widget build(BuildContext context) {
    var controller = Get.find<RecipeDetailController>(tag: itemId);

    _toggleFavorite() {
      controller.toggleFavorite(widget.item?.databaseId);
    }

    return Material(
      child: InkWell(
        onTap: _toggleFavorite,
        child: Container(
          child: Obx(() { // ====**** ERROR IN THIS LINE ****====
            if (controller.isLoading) {
              return CircularProgressIndicator();
            }

            if (controller.resultToggleFavorite?.isUserFavorite ?? false) {
              return Icon(
                Icons.favorite,
              );
            }

            return Icon(
              Icons.favorite_border,
            );
          }),
        ),
      ),
    );
  }
}

在我的案例中使用 Obx 的正确方法是什么?正确的代码是什么?

感谢您的帮助。

您的控制器变量似乎 none 是可观察的(例如,isLoading)。它们不是 Rx 类型,而是普通的 dart 类型。 Obx 或 GetX 仅用于 observables(.obs) 或 Rx 变量。它们就像 StreamBuilder。

因此,如果您不使用可观察对象,则无需使用 Obx 或 GetX。或者你可以让你的变量可观察。