Dika
Dika

Reputation: 2442

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

please help me. What did I do wrong? I got error: [Get] the improper use of a GetX has been detected.

This is the code:

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,
            );
          }),
        ),
      ),
    );
  }
}

What is the proper method to use Obx in my case? What is the correct code?

Thanks for any help.

Upvotes: 2

Views: 7239

Answers (1)

S. M. JAHANGIR
S. M. JAHANGIR

Reputation: 5020

It seems none of your controller variable is observable (for example, isLoading). Instead of being Rx types they are normal dart types. Obx or GetX is only used for observables(.obs) or Rx variables only. They are like StreamBuilder.

Therefore if you aren't using observables, no need to use Obx or GetX. Or you can make your variables observables.

Upvotes: 7

Related Questions