Reputation: 9
I am creating an app that calls a recipe API with user-selected parameters and then returns a List of results. However, I am having trouble returning more than one result. Here is the code I have:
//this is the recipe model
public class RecipeModel
{
public int Id { get; set; }
public string Title { get; set; }
public string Image { get; set; }
}
//this is the Result model
//I had to make a model of a model because the result is a nested JSON response
public class RecipeResultModel
{
public List<RecipeModel> Results { get; set; }
}
//this is the method of making the API call
public static async Task<List<RecipeModel>> LoadRecipes(string queryString)
{
var url = $"{ queryString }";
using (var response = await ApiHelper.ApiClient.GetAsync(url))
{
if (response.IsSuccessStatusCode)
{
RecipeResultModel result = await response.Content.ReadAsAsync<RecipeResultModel>();
return result.Results;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
//here is method that displays the Recipe results
///EDITED CODE
private BindingList<RecipeResultModel> _results;
public BindingList<RecipeResultModel> Results
{
get
{
return _results;
}
set
{
_results = value;
}
}
private async Task<List<RecipeResultModel>> ShowResults()
{
var recipeList = _mapper.Map<List<RecipeResultModel>>(recipeInfo);
ResultList = new BindingList<RecipeResultModel>(recipeList);
ResultList.ItemsSource = recipeList;
}
I can successfully get a single result back from ShowResults(), however I am needing a List.
Upvotes: 0
Views: 1816
Reputation: 65692
When you have a response from an Aysnc/Await call you need to specify the .Result
at the end.
See this .Result
at the end of the await call:
RecipeResultModel result = await response.Content.ReadAsAsync<RecipeResultModel>().Result;
return result.Results;
Given the keyword go ahead and refactor your code not to use the word .Results
.
public class RecipeResultModel
{
public List<RecipeModel> Recipes { get; set; }
}
RecipeResultModel result = await response.Content.ReadAsAsync<RecipeResultModel>().Result;
return result.Recipes;
Upvotes: 0
Reputation: 1374
The return type of Task
on `ShowResults()' means that the function only returns the state of the task -- if it finished or not.
If you want to get a list back from an async Function, you need to add the type you want back after the Task
in the function definition, like you did on the static LoadRecipes
method.
private async Task<List<TheTypeIWant>> ShowResults()
{
var recipeInfo = await RecipeProcessor.LoadRecipes(RecipeUrl);
/*create a list of recipes*/
foreach (var recipe in recipeInfo)
{
var uriSource = new Uri(recipe.Image, UriKind.Absolute);
recipeResult.Text = recipe.Title;
/*add the recipe to the list*/
}
return /*return the list of recipes*/;
}
You need to replace the stuff in comments with the appropriate types for whatever you're trying to do. It's not clear from the question.
You could just return RecipeResults from the static LoadResults() function and the ShowResults() Function
Upvotes: 1