scoped_model icon indicating copy to clipboard operation
scoped_model copied to clipboard

function consecutive running in Scoped Model

Open salehzarei opened this issue 6 years ago • 0 comments

Hi i have problem , when call a Future function from outside of Scoped Model ,that is run and run ... how can i call that just for run once ?

//// function in Scoped model /////

Future fetchSelectedProducts(String categoryid) async {
  print("Run Fetch for $categoryid");
  selectedProductData.clear();
  isLoading = true;
  notifyListeners();
  final response =
      await http.get('https://....');
  List<dynamic> data = json.decode(response.body);
  ProductModel products = ProductModel();
  data.forEach((dynamic protdata) {
    products = ProductModel(
        product_id: protdata['product_id'],
        product_name: protdata['product_name'],
        product_category: protdata['product_category'],
        product_des: protdata['product_des'],
        product_color: protdata['product_color'],
        product_size: protdata['product_size'],
        product_barcode: protdata['product_barcode'],
        product_image: protdata['product_image'],
        product_count: protdata['product_count'],
        product_price_buy: protdata['product_price_buy'],
        product_price_sell: protdata['product_price_sell']);

    if (products.product_category == categoryid) {
      selectedProductData.add(products);
      print("Found:${products.product_category}");
    } else
      print("NotFound");

    notifyListeners();
  });

  isLoading = false;
  notifyListeners();
  return productData;
}

//// call fetchSelectedProducts function in outside widget ////

 @override
  Widget build(BuildContext context) {
    if (widget.currentcategoryID == "0") {
      return Container(
        child: Text("Show All Products"),
      );
    } else {
      runFetchSelectetProduct(widget.currentcategoryID);
      print(
          "Run GridView Product lentgh : ${widget.model.selectedProductData.length} ");
      return Container(
        child: Text("Show ${widget.currentcategoryID} Product"),
      );
       }
  }
}

runFetchSelectetProduct(categoryid) {
    widget.model.fetchSelectedProducts(categoryid);
 
    print("Run Fetch Selected Product Id:$categoryid");
  }

salehzarei avatar Dec 01 '18 19:12 salehzarei