0

ANYONE HELP ME SOLVE THIS ERROR: I have a page that shows a detailed clothes product. But when I complete the detailed product, then I click an add (+) button to increase the number of items, and at the same time when I clicked an add button, a cart button on the left top also increases the value and after that, I pop back to the previous page I get the below error. My app works all fine and is good with no breaks, but this below error bothers me.

This is a detailed product page :

enter image description here

This is an Error when I pop back and click a card product:

enter image description here

This is my detailed products code :

import 'package:zeus_app/detailedPage/addtocartbutton.dart';
import 'package:zeus_app/detailedPage/cartcontrol.dart';
import 'package:zeus_app/detailedPage/colors.dart';
import 'package:zeus_app/detailedPage/size.dart';
import 'package:zeus_app/manClothes/sominam.dart';
import 'package:badges/badges.dart';
import 'package:get/get.dart';
import 'package:zeus_app/product/product.dart';

class DetailProductCart extends StatefulWidget {
  final SoMiNam manSoMi;
  const DetailProductCart({super.key, required this.manSoMi});

  // FavourCounterController favourCounterController = Get.find();
  @override
  _DetailProductState createState() => _DetailProductState();
}

class _DetailProductState extends State<DetailProductCart> {
  CartController cartController = Get.find();
  

  void addPostFrameCallback(FrameCallback callback) {}
  @override
  Widget build(BuildContext context) {
    cartController.initQuantity();
    var size = MediaQuery.of(context).size;
    return Scaffold(
      backgroundColor: Colors.white,
      body: ListView(
        padding: EdgeInsets.zero,
        children: <Widget>[
          Stack(
            children: [
              Container(
                width: size.width,
                height: 500,
                decoration: BoxDecoration(
                    image: DecorationImage(
                        image: AssetImage(widget.manSoMi.image),
                        fit: BoxFit.cover)),
              ),
              Padding(
                padding: const EdgeInsets.only(top: 25, left: 10),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    IconButton(
                      onPressed: () {
                        Navigator.of(context).pushNamed('/man');
                      },
                      icon: const Icon(
                        Icons.arrow_back_ios_new_outlined,
                        color: textColorTabs,
                      ),
                    ),
                    Padding(
                      padding: const EdgeInsets.symmetric(
                          horizontal: 25, vertical: 10),
                      child: Row(
                          mainAxisAlignment: MainAxisAlignment.end,
                          children: <Widget>[
                            Badge(
                              badgeContent: const Text("0"),
                              child: const Icon(
                                Icons.favorite_border_outlined,
                                color: textColorTabs,
                                size: 30,
                              ),
                            ),
                            const SizedBox(width: 20),
                            InkWell(
                              child: Badge(
                                badgeContent: Obx(() => Text(
                                    cartController.numOfItem.value.toString())),
                                child: const Icon(
                                  Icons.local_grocery_store_outlined,
                                  color: textColorTabs,
                                  size: 30,
                                ),
                              ),
                            ),
                          ]),
                    ),
                  ],
                ),
              ),
            ],
          ),
          const SizedBox(height: 15),
          Padding(
            padding: const EdgeInsets.only(left: 12, right: 10),
            child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  SizedBox(
                    width: 200,
                    child: Text(
                      widget.manSoMi.title.toUpperCase(),
                      style: GoogleFonts.robotoMono(
                        textStyle: const TextStyle(
                            fontWeight: FontWeight.bold,
                            color: Colors.black,
                            height: 1.5,
                            fontSize: 22),
                      ),
                    ),
                  ),
                  //SizedBox(width: 20),

                  Text(
                    widget.manSoMi.price,
                    style: GoogleFonts.robotoMono(
                      textStyle: const TextStyle(
                          fontWeight: FontWeight.bold,
                          color: Colors.black,
                          height: 1.5,
                          fontSize: 18),
                    ),
                  ),
                ]),
          ),
          const SizedBox(height: 20),
          const ColorsProducts(),
          const SizedBox(height: 20),
          const SizeProducts(),
          const SizedBox(height: 20),
          ProductCounterAndFavIcon(manSoMi: widget.manSoMi),
          const SizedBox(height: 25),
          AddToCart(soMi: widget.manSoMi),
        ],
      ),
    );
  }
}

When I used this code it occurs this error:

Obx(() => Text(cartController.numOfItem.value.toString())),

enter image description here

This is my cart control file code:

import 'package:get/get.dart';
import 'package:zeus_app/manClothes/sominam.dart';

class CartController extends GetxController {
  var numOfItem = 1.obs;
  var total = 0.obs;
  var cartItems = <SoMiNam>[].obs;
  void removeItem() {
    if (numOfItem.value > 1) {
      numOfItem.value--;
    }
  }

  void addItem() {
    numOfItem.value++;
  }

  void addItemInCart(SoMiNam aoSoMi) {
    cartItems.add(aoSoMi);
    total.value = total.value + numOfItem.value;
    numOfItem.value = 1;
  }

  void initQuantity() {
    numOfItem.value = 1;
  }
}
destructor
  • 41
  • 5
  • It seems your problem is solved here. https://stackoverflow.com/questions/66100385/flutter-setstate-or-markneedsbuild-called-during-build-using-future-builde – learner Oct 04 '22 at 14:45
  • Thank you! I've tried that way but it still has the same error – destructor Oct 04 '22 at 14:55
  • Setting the value of an observable triggers a rebuild of the widget. You can't trigger a rebuild in the middle of a build. You need to move `initQuantity` somewhere it won't be called during a build. Having said that, what is the purpose of that function at all? All it's doing is setting `numOfItem` to 1, but you already did that at `numOfItem`'s declaration. – Abion47 Oct 04 '22 at 16:00

0 Answers0