skip to Main Content
class Counter with ChangeNotifier {
  int justdoit() {
    for (var i = 0; i < orderUp.length; i++) {
      tempList.add(orderUp[i].selectVar.amount);
    }

    int count =
        tempList.fold(0, (previousValue, element) => previousValue + element);
    return count;
  }

}

The provider is supposed to get all amounts from orderUp which are ints and are put in another list which uses fold to find the sum of all values in tempList and then return it.

List<int> tempList = [];

I’m trying to return on this widget

Container(
                padding: const EdgeInsets.all(15),
                decoration: const BoxDecoration(
                  color: Colors.white,
                  shape: BoxShape.circle,
                ),
                child: Text(
                  context.watch<Counter>().justdoit().toString(),
                  style: const TextStyle(
                    color: Colors.black,
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),

But I don’t get anything. I always get ‘0’

class DetailPage extends StatefulWidget {
  final Food food;
  DetailPage(this.food);

  @override
  State<DetailPage> createState() => _DetailPageState();
}

class _DetailPageState extends State<DetailPage> {
  @override
  Widget build(BuildContext context) {
    context.watch<Counter>();
    return Scaffold(
      appBar: AppBar(
        leadingWidth: 65,
        backgroundColor: Colors.transparent,
        leading: Padding(
          padding: const EdgeInsets.only(left: 24.0),
          child: Container(
            // ignore: prefer_const_constructors
            decoration: BoxDecoration(
              shape: BoxShape.circle,
              color: Colors.white,
            ),
            width: 40,
            height: 40,
            child: IconButton(
              icon: const Icon(Icons.arrow_back_ios_outlined),
              color: Colors.black,
              onPressed: () => Navigator.of(context).pop(),
            ),
          ),
        ),
        actions: [
          Padding(
            padding: const EdgeInsets.only(right: 24.0),
            child: Container(
              // ignore: prefer_const_constructors
              decoration: BoxDecoration(
                shape: BoxShape.circle,
                color: Colors.white,
              ),
              width: 40,
              height: 40,
              child: IconButton(
                icon: Icon(
                  ((context.read<FavFood>().favorited(widget.food) == false)
                      ? Icons.favorite_outline
                      : Icons.favorite),
                  color: Colors.black,
                ),
                onPressed: () {
                  bool click2 = context.read<FavFood>().favorited(widget.food);
                  (context.read<FavFood>().rev(widget.food));
                  (context.read<FavFood>().favorited(widget.food));
                  setState(() {
                    click2 = !click2;
                  });
                  context.read<FavFood>().favorited(widget.food);
                },
              ),
            ),
          ),
        ],
      ),
      backgroundColor: Color(0xff453658),
      body: SingleChildScrollView(
        child: Column(
          children: [
            FoodImg(widget.food),
            FoodDetail(widget.food),
          ],
        ),
      ),
      floatingActionButton: SizedBox(
        width: 100,
        height: 56,
        child: RawMaterialButton(
          fillColor: const Color(0xff453658),
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(50),
          ),
          elevation: 2,
          child: Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: [
              IconButton(
                icon: const Icon(Icons.shopping_cart_outlined),
                color: Colors.black,
                onPressed: () {
                  Navigator.of(context).pushNamed(cartRoute);
                  context.read<TotalPrice>().update();
                },
              ),
              Container(
                padding: const EdgeInsets.all(15),
                decoration: const BoxDecoration(
                  color: Colors.white,
                  shape: BoxShape.circle,
                ),
                child: Text(
                  context.watch<Counter>().justdoit().toString(),
                  style: const TextStyle(
                    color: Colors.black,
                    fontSize: 18,
                    fontWeight: FontWeight.bold,
                  ),
                ),
              ),
            ],
          ),
          onPressed: () {},
        ),
      ),
    );
  }
}

2

Answers


  1. I’ve checked the code, it would be better not to use global variable. And as for the update you need to notifyListner to update the ui. For demonstration purpose,

    You can create another variable

    class Counter with ChangeNotifier {
      int justdoitValue = 0;
      int justdoit() {
        print(orderUp.length);
        for (var i = 0; i < orderUp.length; i++) {
          bruhhh.add(orderUp[i].selectVar.amount);
        }
    
        int count =
            bruhhh.fold(0, (previousValue, element) => previousValue + element);
        justdoitValue = count;
        notifyListeners(); // depends on what do you want
        return count;
      }
    

    And call on food_quantity.dart#L137

     GestureDetector(
       onTap: () {
       ..........
       context.read<Counter>().justdoit();
       }
    

    And use lib/screens/detail/detail.dart#L110

    child: Text(
         context.watch<Counter>().justdoitValue.toString(),
    

    And I think you may restructure the logic.

    Login or Signup to reply.
  2. You coule use those code, it would be better not to use global variable. And as for the update you need to notifyListner to update the ui. For demonstration purpose,

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search