Xamarin Forms Httpclient PostAsync returns null

VipinKrishnaVipinKrishna Member ✭✭
edited February 5 in Xamarin.Forms

Homepage showing products in a list according to the category selected.Initially showing products of first category. API response is null for first category but count is correct,showing only blank frames. After switching from other categories, products are shown correctly.Awaited the tasks. I think the problem is with async-await calls.Help need to rectify this..

        private async Task InitializeAsync()
                {
                    await Get_categories(); //calling method
                }
                public Task Initialization { get; private set; }       

                public  Grid_vm() //constructor
                {
                    Initialization = InitializeAsync();
        }


public async Task Get_categories()
        {                
            var items = await restClient.Get_categories();                           
            Categorylist = new List<Category_value>();                    
                foreach (Category_data cat in items.data)
                {
                    Category_value category = new Category_value();
                    if (cat.category_id=="6")
                    {
                        category.category_id = cat.category_id;
                        category.category_name = cat.category_name;                            
                        Categorylist.Add(category);    
                    }
                    else
                    {
                        category.category_id = cat.category_id;
                        category.category_name = cat.category_name;                            
                        Categorylist.Add(category);    
                    }
                }
                await Get_branch_products("6") ;  // calling first category                
        }


public async Task Get_branch_products (string category_id)
        {                             
            RestClient restClient = new RestClient();
        //calling restAPI//var items=await restClient.Get_branch_products2(category_id);                               
        //var items null initially// Productlist = new List<Product_Value2>();
                    foreach (Product_Data p in items.data)
                    {
                        Product_Value2 val = new Product_Value2();
                        val.available_qty = p.available_qty;

                        if (p.status == "1")
                        {

                            Productlist.Add(val);
                        }
                        else
                        {

                            Productlist.Add(val);
                        }
                    }               
            }
        }



 public async Task<ProductDetails> Get_branch_products2(string cat_id)
                {                
                           var formcontent = new FormUrlEncodedContent(new[]
                            {
                                new KeyValuePair<string,string>("category_id",cat_id)
                            });    
    //execution returns null from here//var response = await client.PostAsync(base_url + "listbranchproduct", formcontent).ConfigureAwait(false);
  //executed later//  var result = await response.Content.ReadAsStringAsync();
                            var status = JsonConvert.DeserializeObject<ProductDetails>(result);
                            return status;                               
                }
Sign In or Register to comment.