Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

function consecutive running in Scoped Model #48

Open
salehzarei opened this issue Dec 1, 2018 · 0 comments
Open

function consecutive running in Scoped Model #48

salehzarei opened this issue Dec 1, 2018 · 0 comments

Comments

@salehzarei
Copy link

salehzarei commented Dec 1, 2018

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 salehzarei changed the title consecutive running function function consecutive running in Scoped Model Dec 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant