简体   繁体   中英

Xamarin Forms Httpclient PostAsync returns null

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();
            var items=await restClient.Get_branch_products2(category_id);//calling restAPI                               
            Productlist = new List<Product_Value2>();//var items null initially
                    foreach (Product_Data p in items.data)
                    {
                        Product_Value2 val = new Product_Value2();
                        val.available_qty = p.available_qty;                            
                        if (p.status == "1")
                        {        
                             val.visible = true ;
                             OnPropertyChanged("visible");
                             val.visibility = false;
                             OnPropertyChanged("visibility");                                  
                             Productlist.Add(val);
                        }
                        else
                        {    
                             val.visible = true ;
                             OnPropertyChanged("visible");
                             val.visibility = false;
                             OnPropertyChanged("visibility"); 
                             val.opacity = 0.25;
                             OnPropertyChanged("opacity");                           
                             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)
                            });    
      var response = await client.PostAsync(base_url + "listbranchproduct", formcontent).ConfigureAwait(false);//execution returns null from here
      var result = await response.Content.ReadAsStringAsync();//executed later  
                            var status = JsonConvert.DeserializeObject<ProductDetails>(result);
                            return status;                               
                }

<ListView  x:Name="pdt_list" HasUnevenRows="True" SeparatorVisibility="None" ItemsSource="{Binding Productlist}" BackgroundColor="White" Margin="0,0,0,0">
                    <ListView.ItemTemplate>
                        <DataTemplate >
                            <ViewCell >
                                <ViewCell.View>
                                    <Frame HasShadow="False" Margin=" 0,10,0,10"  Padding="10,0,10,5" BackgroundColor="#f1f1f1" HeightRequest="80" HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand" >
                                        <Grid Padding="0">
                                            <!--<StackLayout HorizontalOptions="FillAndExpand" VerticalOptions="CenterAndExpand"  Margin="0,10,0,10"  >-->
                                            <StackLayout VerticalOptions="FillAndExpand" Margin="0" Padding="10,0,0,0" Orientation="Horizontal" Opacity="{Binding opacity}">
                                                <Image Source="{Binding image}"  Aspect="AspectFill"  WidthRequest="70" HeightRequest="180"  VerticalOptions="FillAndExpand" />
                                                <StackLayout HorizontalOptions="FillAndExpand" Orientation="Vertical"  >
                                                    <Label Text="{Binding Name}" Font="Bold" VerticalTextAlignment="Center" FontSize="Small" TextColor="Black" FontFamily="opensans_light.ttf#opensans_light" Margin="10,20,0,0" />
                                                    <StackLayout Orientation="Horizontal" Margin="10,0,0,0" HorizontalOptions="Start" VerticalOptions="Start"  >
                                                        <Label Text="{Binding sellingprice_default}" Font="Bold"  HorizontalOptions="Start" Margin="0" TextColor="#FA2E27" VerticalTextAlignment="Start" FontSize="Small" FontFamily="opensans_light.ttf#opensans_light" />
                                                        <Label Text="QAR"  TextColor="#FA2E27" HeightRequest="90" FontSize="Small" HorizontalOptions="Start" VerticalTextAlignment="Start" VerticalOptions="FillAndExpand" />
                                                        </StackLayout>
                                                        </StackLayout>
                                                        <ImageButton Source="carts.png" BackgroundColor="Transparent" IsVisible="{Binding visible}" HorizontalOptions="EndAndExpand" WidthRequest="40" HeightRequest="40" VerticalOptions="CenterAndExpand"  Clicked="Add_cart_Clicked" Margin="0,20,0,0" Aspect="AspectFit">
                                                        </ImageButton>
                                                    <StackLayout Orientation="Horizontal" BackgroundColor="Transparent" IsVisible="{Binding visibility}" HorizontalOptions="EndAndExpand">
                                                        <ImageButton Source="minus.png" HorizontalOptions="End" VerticalOptions="Center"   Aspect="Fill"   BackgroundColor="Transparent"  WidthRequest="20" HeightRequest="20" Clicked="Minus_Tapped" />
                                                        <Label Text="{Binding Number2}" VerticalTextAlignment="Center" HorizontalOptions="EndAndExpand"   FontSize="Medium" TextColor="Black" FontFamily="opensans_light.ttf#opensans_light" Margin="0,0,0,0" />
                                                        <ImageButton Source="plus2.png" HorizontalOptions="End" Aspect="Fill" VerticalOptions="Center"  BackgroundColor="Transparent"   WidthRequest="20" HeightRequest="20"  Clicked="Plus_Tapped"   />
                                                    </StackLayout>
                                              </StackLayout>
                                             <StackLayout BackgroundColor="Black" HorizontalOptions="EndAndExpand" VerticalOptions="StartAndExpand" WidthRequest="100" HeightRequest="25" IsVisible="{Binding opaque}" Margin="0,0,0,0" >

                                                <Label Text="Not Available" FontFamily="opensans_light.ttf#opensans_light" TextColor="White" FontAttributes="Bold" HorizontalOptions="Center" VerticalTextAlignment="Center" />
                                            </StackLayout>
                                        </Grid>
                                    </Frame>
                                </ViewCell.View>
                            </ViewCell>
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>

opacity was provided inside the Get_branch_products (string category_id) method else block as 0.25.

val.opacity = 1; 
OnPropertyChanged("opacity"); 

was not given inside if block adding it there solved the issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM