I want to get the quantity of particular product on another page on click of button present in listviewbuilder in Flutter application

Below in my MenuItemList code from where i have to fetch quantity.

```import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'custom_wedget.dart';
import 'customization_screen.dart';

class MyHomePage extends StatefulWidget {
  final String rest_id;
  MyHomePage(this.rest_id, {Key key, this.title}) : super(key: key);
      final String title;
      @override
  _MyHomePageState createState() => _MyHomePageState();
}
    class _MyHomePageState extends State<MyHomePage> {
  TextEditingController customNumberButtonController =
  new TextEditingController();
  TextEditingController _specialInstuctionController = TextEditingController();
  int _counter =1;
  int _selected_index=0;
  int itemcounter = 0;
  int tempOrderMasterId;
  List userData;
  List userData1;
  List userData2;
  List data;
  List data1;
  List data3;
  String menu_category_restaurant_id='0';
  Future<List> getData() async {
    Map data2 = {"restaurant_id": widget.rest_id};
    String body2 = json.encode(data2);

    String myUrl =

        "myapi4";
    http.Response response1 = await http.post(myUrl,
        headers: {"Accept": "Application/json"}, body: body2);

    myUrl = "myapi5";
    http.Response response2 = await http.post(myUrl,
        headers: {"Accept": "Application/json"}, body: body2);


    data1 = json.decode(response1.body);
    data3 = json.decode(response2.body);

    setState(() {
      userData1 = data1;
      userData2 = data3;
    });

return data;

  }

Future getMenuData() async {
Map data2 = {“restaurant_id”: widget.rest_id,“menu_category_restaurant_id”:menu_category_restaurant_id};
String body2 = json.encode(data2);
String myUrl =
“my api 5”;
http.Response response = await http.post(myUrl,
headers: {“Accept”: “Application/json”}, body: body2);
data = json.decode(response.body);

    setState(() {          userData = data;         });

return data;
}

  @override
  void initState() {
    super.initState();
    /*for (var i = 0; i < userData.length; i++) {
      customNumberButtonController.add(TextEditingController());
    }*/
    getData();
    getMenuData();
    getTotalItem();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(""),
        backgroundColor: Colors.green,
      ),

      drawer: MainDrawer(),
      body: Column(
        children: [
          Container(
            child: Padding(
              padding: const EdgeInsets.all(8.0),
              child: Text("Menu",style: TextStyle(fontSize: 20),),
            ),
          ),
          Container(
            child: userData != null
                ?  Expanded(
              child: ListView.builder(
                itemCount: userData == null ? 0 : userData.length,
                itemBuilder: (BuildContext context, int index) {



                  return Container(
                    margin: EdgeInsets.only(left: 10),
                    child: Row(
                      children: <Widget>[
                        Card(
                          elevation: 5,
                          child: Container(
                            // color: Colors.amber[50],
                            //padding: EdgeInsets.all(10),
                            child: Row(
                              children: [
                                Container(
                                  margin: EdgeInsets.all(5),
                                  width: 20,
                                  child: Center(
                                    child: CustomImage(
                                        imageWidth:
                                        MediaQuery.of(context).size.width *
                                            .80,
                                        imageUrl:
                                        "https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcSnjSaOG0_tIO2md739srmHEV_nrny0GGBpVA&usqp=CAU"),
                                  ),
                                ),
                                Container(
                                  margin: EdgeInsets.all(5),
                                  width: MediaQuery.of(context).size.width * .40,
                                  // color: Colors.cyanAccent[100],
                                  child: Column(
                                    children: [
                                      Padding(
                                        padding: EdgeInsets.all(3),
                                      ),
                                      Align(
                                        alignment: Alignment.centerLeft,
                                        child: Text(
                                          "${userData[index]["menu_item_name"]}",
                                          style: TextStyle(
                                              fontSize: 14,
                                              color: Colors.black,
                                              fontWeight: FontWeight.bold),
                                        ),
                                      ),
                                      Align(
                                        alignment: Alignment.centerLeft,
                                        child: Text(
                                          "${userData[index]["menu_item_description"]}",
                                          style: TextStyle(
                                              fontSize: 13, color: Colors.black),
                                        ),
                                      ),
                                      Align(
                                        alignment: Alignment.centerLeft,
                                        child: Text(
                                          "${userData[index]["menu_item_cost"]}",
                                          style: TextStyle(
                                              fontSize: 14,
                                              color: Colors.black,
                                              fontWeight: FontWeight.bold),
                                        ),
                                      ),
                                    ],
                                  ),
                                ),
                                Container(
                                  height: 25,
                                  margin: EdgeInsets.all(5),
                                  width: MediaQuery.of(context).size.width * .25,
                                  child:/*Row(
                                    children: [
                                      Container(
                                          width: 25,
                                          height: 25,
                                          child: RaisedButton(
                                            padding: EdgeInsets.zero,
                                            onPressed: _descrementCounter(index),
                                            color: Colors.red,
                                            child: Column(
                                              // Replace with a Row for horizontal icon + text
                                              children: <Widget>[
                                                Icon(Icons.indeterminate_check_box),
                                              ],
                                            ),
                                          )),
                                      Container(
                                        width: 35,
                                        height: 25,
                                         child: Align(
                                           alignment: Alignment.center,
                                           child: TextField(
                                             controller: customNumberButtonController[index],
                                             // obscureText: true,
                                             //textAlign: TextAlign.left,
                                           ),
                                         ),
                                        *//*child: Align(
                                          alignment: Alignment.center,
                                          child: Text(
                                            '$_counter',
                                            style: Theme.of(context).textTheme.headline6,
                                          ),
                                        ),*//*
                                        //controller: widget.customNumberButtonController,
                                      ),
                                      Container(
                                        width: 25,
                                        height: 25,
                                        child: RaisedButton(
                                          padding: EdgeInsets.zero,
                                          onPressed: _incrementCounter(index),
                                          color: Colors.green[300],
                                          child: Column(
                                            // Replace with a Row for horizontal icon + text
                                            children: <Widget>[
                                              Icon(Icons.add_box),
                                            ],
                                          ),
                                        ),
                                      ),
                                    ],
                                  ),*/
                                  CustomNumberButton(
                                      customNumberButtonController, 0),
                                ),
                                Align(
                                  child: Container(
                                    height: 25,
                                    margin: EdgeInsets.all(5),
                                    width: 60,
                                    child: Center(
                                      child: RaisedButton(
                                        padding: EdgeInsets.zero,
                                        shape: RoundedRectangleBorder(
                                            borderRadius:
                                            BorderRadius.circular(5.0)),
                                        child: Title(
                                          color: Colors.black,
                                          child:
                                          ("${userData[index]["customize_flag"]}" ==
                                              'Y')
                                              ? Text(
                                            'Customize',
                                            style: TextStyle(
                                                color: Colors.white,
                                                fontSize: 11),
                                          )
                                              : Text(
                                            'Add',
                                            style: TextStyle(
                                                color: Colors.white,
                                                fontSize: 11),
                                          ),
                                        ),
                                        onPressed: () {
                                          if ("${userData[index]["customize_flag"]}" ==
                                              "Y") {
                                            Navigator.push(context, MaterialPageRoute(builder: (context) => Customisation_screen(int.parse(this.customNumberButtonController.text),"${userData[index]["menu_item_name"]}","${userData[index]["menu_item_master_id"]}"),));
                                          } else {
                                            _displayDialog(
                                                context,
                                                "${userData[index]["menu_item_name"]}",
                                                customNumberButtonController.text,
                                                "${userData[index]["menu_item_master_id"]}");
                                            setState(() {
                                              //itemcounter += 1;
                                              print(customNumberButtonController
                                                  .text);
                                              print("/");
                                              print(
                                                  "${userData[index]["menu_item_master_id"]}");
                                            });
                                          }
                                        },
                                        color: Colors.green[900],
                                      ),
                                    ),
                                  ),
                                )
                              ],
                            ),
                          ),
                        ),
                      ],
                    ),
                  );
                },
              ),
            ):Expanded(
              child: Container(

                child: Center(
                  child: CircularProgressIndicator(),
                ),
              ),
            ),
          ),
          itemcounter > 0
              ? Container(
            color: Colors.green[400],
            child: Row(
              //mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Container(
                  margin: EdgeInsets.only(left: 20),
                  child: Text(
                    " In Cart: ",
                    style: TextStyle(fontSize: 18.0, color: Colors.white),
                  ),
                ),
                Container(
                  margin: EdgeInsets.only(left: 5, right: 50),
                  child: Text(
                    "$itemcounter Items",
                    style: TextStyle(fontSize: 18.0, color: Colors.white),
                  ),
                ),
                Container(
                  margin: EdgeInsets.symmetric(horizontal: 10),
                  child: RaisedButton(
                      color: Colors.green[900],
                      child: Text(
                        "Review order",
                        style: TextStyle(
                            fontSize: 18.0, color: Colors.white),
                      ),
                      onPressed: () {
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                              builder: (context) => ItemDescription(
                                  itemcounter, tempOrderMasterId )),
                        );
                      }),
                ),
              ],
            ),
          )
              : null,
        ],
      ),
    );
  }
}```


Below is my quantity Widget 

    ```import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    class CustomCategoryCard extends StatefulWidget {
      final String foodCategory;
      final int selected_index;
      final int current_index;
      CustomCategoryCard({this.foodCategory,this.selected_index,this.current_index});
    
      @override
      _CustomCategoryCardState createState() => _CustomCategoryCardState();
    }
    
    class _CustomCategoryCardState extends State<CustomCategoryCard> {
      @override
      Widget build(BuildContext context) {
        return Card(
          elevation: 5,
          child: Container(
            color: widget.selected_index != null && widget.selected_index == widget.current_index
                ? Colors.green
                : Colors.white,
            padding: EdgeInsets.symmetric(vertical: 0, horizontal: 10),
            child: Align(
              alignment: Alignment.center,
              child: Text(
                widget.foodCategory,
                style: TextStyle(
                    fontSize: 16.0,
                    color: widget.selected_index != null && widget.selected_index == widget.current_index
                        ? Colors.white
                        : Colors.black,
                    fontWeight: FontWeight.bold),
              ),
            ),
          ),
        );
      }
    }
    
    class CustomImage extends StatefulWidget {
      final double imageWidth;
      final String imageUrl;
      CustomImage({this.imageWidth, this.imageUrl});
    
      @override
      _CustomImageState createState() => _CustomImageState();
    }
    
    class _CustomImageState extends State<CustomImage> {
      @override
      Widget build(BuildContext context) {
        return Image(
          width: widget.imageWidth,
          image: NetworkImage(widget.imageUrl),
        );
      }
    }
    }
    class CustomNumberButton extends StatefulWidget {
      CustomNumberButton(this.customNumberButtonController, this.qty);
      final customNumberButtonController;
      final qty;
    
      @override
      _CustomNumberButtonState createState() => _CustomNumberButtonState();
    }
    
    class _CustomNumberButtonState extends State<CustomNumberButton> {
      int _counter = 0;
    
      @override
      void initState() {
        if(widget.qty==0){
          _counter = 1;
        }else {
          _counter = widget.qty;
        }
    
        super.initState();
      }
    
      void _incrementCounter() {
        setState(() {
          _counter++;
        });
      }
    
      void _descrementCounter() {
        setState(() {
          if (_counter > 1) {
            _counter--;
          }
        });
      }
    
      @override
      Widget build(BuildContext context) {
        widget.customNumberButtonController.text = '$_counter';
        return Container(
          child: Row(
            children: [
              Container(
                  width: 25,
                  height: 25,
                  child: RaisedButton(
                    padding: EdgeInsets.zero,
                    onPressed: _descrementCounter,
                    color: Colors.red,
                    child: Column(
                      children: <Widget>[
                        Icon(Icons.indeterminate_check_box),
                      ],
                    ),
                  )),
              Container(
                width: 35,
                height: 25,
                child: Align(
                  alignment: Alignment.center,
                  child: Text(
                    '$_counter',
                    style: Theme.of(context).textTheme.headline6,
                  ),
                ),
                //controller: widget.customNumberButtonController,
              ),
              Container(
                width: 25,
                height: 25,
                child: RaisedButton(
                  padding: EdgeInsets.zero,
                  onPressed: _incrementCounter,
                  color: Colors.green[300],
                  child: Column(
                    // Replace with a Row for horizontal icon + text
                    children: <Widget>[
                      Icon(Icons.add_box),
                    ],
                  ),
                ),
              ),
            ],
          ),
        );
      }
    }
    
    //////
    class CustomigeBox extends StatefulWidget {
      @override
      _CustomigeBoxState createState() => _CustomigeBoxState();
    }
    
    class _CustomigeBoxState extends State<CustomigeBox> {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            //title: Text("Second Route"),
          ),
          body: Center(
            child: RaisedButton(
              onPressed: () {
                Navigator.pop(context);
              },
              child: Text('Go back!'),
            ),
          ),
        );
      }
    }```

I want to get quantity value on another page with following code.

```import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import './second_level_menu.dart';

import 'custom_wedget.dart';

class Customisation_screen extends StatefulWidget {
  final String menu_item_master_id;
  final int quantity;
  final String menu_item_name;

  Customisation_screen(this.quantity,this.menu_item_name,this.menu_item_master_id,);
  @override
  _Customisation_screenState createState() => _Customisation_screenState(quantity,menu_item_name,menu_item_master_id);
}

class _Customisation_screenState extends State<Customisation_screen> {
  TextEditingController customNumberButtonController =
  new TextEditingController();
  final String menu_item_master_id;
  final int quantity;
  final String menu_item_name;
  _Customisation_screenState(this.quantity,this.menu_item_name,this.menu_item_master_id);

  List menu_data;

  @override
  void initState() {
    super.initState();
    this.getData();
    print(quantity);
  }

  Future<String> getData() async {
    Map data2 = {"menu_item_master_id": menu_item_master_id};
    String body2 = json.encode(data2);

    String myUrl =
        "my api 3";
    http.Response response = await http.post(myUrl,
        headers: {"Accept": "Application/json"}, body: body2);

    setState(() {
      menu_data = json.decode(response.body);
    });

    return 'success';
  }
  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(backgroundColor: Colors.green,),
      body: Column(
        children: <Widget>[
          Text('Customize your Menu',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 25),),
          SizedBox(height: 10,),
          Expanded(
            child: Container(
              margin: EdgeInsets.only(top: 20),
              child: ListView.builder(
                itemCount: menu_data==null ? 0: menu_data.length,
                itemBuilder: (BuildContext context,int index){
                  return Container(
                    child: Column(
                      children: <Widget>[
                        SizedBox(height: 10,),
                        Row(
                          children: <Widget>[
                            Expanded(child: Padding(
                              padding: const EdgeInsets.only(left: 10),
                              child: Text(menu_data[index]['customization_items_title'],style: TextStyle(fontWeight: FontWeight.bold,fontSize: 20,color: Colors.red),),
                            )),

                            Expanded(
                              child: Container(child: (menu_data[index]['customization_items_type']=='S')?
                              Padding(
                                padding: const EdgeInsets.only(right: 10),
                                child: Text('Only 1 item allowed', style: TextStyle(fontSize: 15,),textAlign: TextAlign.right,),
                              ):(menu_data[index]['max_selection'] != '0')?
                              Text('Only '+ menu_data[index]['max_selection']+' item allowed', style: TextStyle(fontSize: 15,),textAlign: TextAlign.right,): null,),
                            ),
                          ],
                        ),
                        SizedBox(height: 5,),
                        Row(
                          children: <Widget>[
                            Expanded(child: Padding(
                              padding: const EdgeInsets.only(left: 10),
                              child: (menu_data[index]['must_enter']=='Y')?
                              Text('Required',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15,color: Colors.red),):
                              Text('Optional',style: TextStyle(fontWeight: FontWeight.bold,fontSize: 15,color: Colors.red),),
                            )),

                            Container(child: (menu_data[index]['price_applicable']=='C')?
                            Padding(
                              padding: const EdgeInsets.only(right: 10),
                              child: Text(menu_data[index]['customization_items_price'], style: TextStyle(fontSize: 15,),textAlign: TextAlign.right,),
                            ): null,),
                          ],
                        ),
                        Divider(thickness: 2,),
                        Container(
                          child: Second_level_menu(menu_data[index]['customization_items_id'],menu_data[index]['customization_items_type']),
                        )
                        //Second_level_menu()
                      ],
                    ),
                  );
                },

              ),
            ),
          ),
          Container(
            margin: EdgeInsets.all(8),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(8.0),
              color: Colors.grey[300],
              boxShadow: [
                BoxShadow(
                  color: Colors.black,
                  blurRadius: 2.0,
                  spreadRadius: 0.0,
                  offset: Offset(2.0, 2.0), // shadow direction: bottom right
                )
              ],
            ),
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceEvenly,
              children: <Widget>[
                Container(
                  height: 25,
                  margin: EdgeInsets.all(5),
                  width: MediaQuery.of(context).size.width * .25,
                  child: CustomNumberButton(
                      customNumberButtonController, quantity),
                ),
                Container(
                  child: RaisedButton(
                      color: Colors.green[900],
                      child: Text('Add Item',style: TextStyle(color: Colors.white),),
                      onPressed: (){}),
                )
              ],
            ),
          )
        ],
      ),
    );
  }
}```

Hey @shubham_mishra. I see that you are making a food delivery app :slight_smile: Or an ecommerce app at it’s core. For such applications, App State management is crucial.
Docs.

I would suggest that you learn about ScopedModel/Provider whichever you are comfortable with as they are relatively less complex and you should be able to pick it up soon.

Nevertheless, if you are at a time crunch then I can guide you a bit, highly recommend you to study about state management tho.

I see below that you have passed quantity to Customisation_screen, where is the call for this? And what value are you getting here?

1 Like