Search code examples
flutterdart

How can I refresh data immediately after updating it in flutter?


Here I'm trying to to update quantity in product_inventory table that links between products and inventories,
however it's updated in the database but not in the screen until I use ctrl+S.
I tried to use many possible ways as calling the quantity in different way even in setState but none of them worked, how can I make the data change after the quantity update.
also I'm using an API with laravel

class ProductsInInventoryWidget extends StatefulWidget
{
  ProductsInInventory? model;
  BuildContext? context;
  //final ValueChanged onPressed;

  ProductsInInventoryWidget({this.model,this.context});

  @override
  _ProductsInInventoryWidgetState createState() => _ProductsInInventoryWidgetState();
}



class _ProductsInInventoryWidgetState extends State<ProductsInInventoryWidget> {

  var apiCall = new APICall();
   addQuantity(String mydata) async
  {
    String Product_BarCode = widget.model!.productBarcode.toString();
    String Inventory_id = widget.model!.inventoryId.toString();
    var response = await http.put(
        Uri.parse("http://192.168.1.188:8000/api/user/IncrementProduct/"+Product_BarCode+"/"+Inventory_id,),
      body:jsonEncode(<String, String>{
        'quantity': mydata,
      }),
    );

     // setState(() {
     //   response;
     //   mydata = widget.model!.quantity.toString();
     // });

  }

  @override
  Widget build(BuildContext context) {
    var quantityData = widget.model!.quantity.toString();
    return InkWell(
        onTap: ()
        {
        //  Navigator.push(context, MaterialPageRoute(builder: (c)=> ProductsInInventoryForm(model: widget.model)));
        },
        splashColor: Colors.amber,
        child:  Padding(
            padding: EdgeInsets.only(top:0.0),
            child : Card(

              margin: EdgeInsets.fromLTRB(5, 6, 5, 0),
              child: Padding(
                padding:  EdgeInsets.fromLTRB(10, 10, 10, 10),
                child: Column(
                  children: [
                    ListTile(
                      leading: CircleAvatar(
                        radius:40,
                        backgroundColor: Colors.black38,
                        child: Text(quantityData
                          ,style: TextStyle(
                          color: Colors.white
                        ),),
                      ),
                      title: Text(widget.model!.productName), //(widget.model!.ProductName!,),
                      subtitle: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [

                          Text("code bar : " + widget.model!.productBarcode ),
                          Text("updated : " +  DateFormat('yyyy-MM-dd kk:mm').format(widget.model!.updatedAt)),
                          Row(
                                    mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              IconButton(
                                icon: Icon(Icons.add,color: Colors.blue,),

                                onPressed: ()async {
                                  onRefresh: () { setState((){
                                    // Update your data here
                                    addQuantity(quantityData);
                                  }); };
                                  setState(() {
                                  //  widget.onPressed;
                                    addQuantity(quantityData);
                                    quantityData = widget.model!.quantity.toString();
                                  });
                                }
                              ),
                              IconButton(
                                icon: Icon(Icons.remove,color: Colors.blue,),
                                onPressed: () {
                                  // Perform some action
                                },

                              ),
                              FlatButton(
                                onPressed: () {
                                  // Perform some action
                                },
                                child: const Text('Delete',style: TextStyle(color: Colors.blue),),
                              ),

                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            )
        )
    );
  }
}

Solution

  • i found the solution i just had to change it in the ui seperatly by changing "quantityData" to an int and increment it in the setstate, this way it keeps updating in the screen

    class ProductsInInventoryWidget extends StatefulWidget
    {
      ProductsInInventory? model;
    
      BuildContext? context;
    
    
    
      ProductsInInventoryWidget({this.model,this.context});
    
      @override
      _ProductsInInventoryWidgetState createState() => _ProductsInInventoryWidgetState();
    }
    
    
    
    class _ProductsInInventoryWidgetState extends State<ProductsInInventoryWidget> {
    
    
    
      int quantityData =1;
    
    
    
    
      Future addQuantity(int mydata) async
      {
        String Product_BarCode = widget.model!.productBarcode.toString();
        String Inventory_id = widget.model!.inventoryId.toString();
        var response = await http.put(
          Uri.parse("http://192.168.1.188:8000/api/user/IncrementProduct/$Product_BarCode/$Inventory_id/$mydata",),
          body:jsonEncode(<String, String>{
            'quantity': mydata.toString(),
          }),
        );
        var body = jsonDecode(response.body);
    
    
    
      }
    
    
      Future<http.Response> DeleteProductFromInventoryProducts(int join_id) async
      {
        final http.Response response = await http.delete(
          Uri.parse("http://192.168.1.188:8000/api/user/deleteProductsFromInventory/$join_id",)
        );
    
        return response;
    
    
      }
    
      @override
      void initState() {
        // TODO: implement initState
        quantityData = widget.model!.quantity; // 2# assign the incoming data here
        super.initState();
    
      }
    
    
      @override
      Widget build(BuildContext context) {
        return Padding(
            padding: EdgeInsets.only(top:0.0),
            child : Card(
    
              margin: EdgeInsets.fromLTRB(5, 6, 5, 0),
              child: Padding(
                padding:  EdgeInsets.fromLTRB(10, 10, 10, 10),
                child: Column(
                  children: [
                    ListTile(
                      leading: CircleAvatar(
                        radius:40,
                        backgroundColor: Colors.black38,
                        child: Text(quantityData.toString()
                          ,style: TextStyle(
                          color: Colors.white
                        ),),
                      ),
                      title: Text(widget.model!.productName), //(widget.model!.ProductName!,),
                      subtitle: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
    
                          Text("code bar : " + widget.model!.productBarcode ),
                          Text("updated : " +  DateFormat('yyyy-MM-dd kk:mm').format(widget.model!.updatedAt)),
                          Row(
                                    mainAxisAlignment: MainAxisAlignment.start,
                            children: [
                              IconButton(
                                icon: Icon(Icons.add,color: Colors.blue,),
    
                                onPressed: () async{
    
                                 // await addQuantity(quantityData);
    
    
    
                                  setState(() {
    
                                    quantityData+=1;
                                    print(quantityData.toString());
                                    addQuantity(quantityData);
                                  });
    
    
                                }
                              ),
                              IconButton(
                                icon: Icon(Icons.remove,color: Colors.blue,),
                                onPressed: () {
                                  // Perform some action
                                },
    
                              ),
                              FlatButton(
                                child: const Text('Delete',style: TextStyle(color: Colors.blue),),
                                onPressed: () {
                                  // Perform some action
                                  setState(() {
                                 //   DeleteProductFromInventoryProducts(widget.model!.id.toString());
                                  });
                                },
    
                              ),
    
                            ],
                          ),
                        ],
                      ),
                    ),
                  ],
                ),
              ),
            )
        );
      }
    }```