Reputation: 470
Hello I have a Database with Recipes. Those have a title, ingredients ando so on. But now I need to convert the data into a object for my app. But here comes the problem I dont know how I can map a list of things in a Object with flutter. The table with the ingredients has three columns a ingredient id which is unique, a recipe id which refers to a recipe so its not unique and the name of the ingredient. I had a way adding it but than I got the error "Uint8ArrayView' is not a subtype of type 'List" to fix this I used .toString() but this doesnt work for the ingredients. Because its of type List and not od type string.
What I already got:
//...
return Recipe(
id: i,
name: parsedTitle[i]['Rezept_Title'].toString(),
ingredients: //Here is where I need help,
preperation: parsedPreperation[i]['Zubereitung'].toString(),
imageUrl: imgUrl[i]['Image_URL'],
);
//...
I hope you can help me. Thank you!
Upvotes: 0
Views: 918
Reputation: 11651
I don't know the json you get from your api but lets say it is
[
{
"id": 1,
"name": "Recipe name1",
"ingredients": [
{
"name": "ingredient 1",
"quantity": "1 tbsp"
},
{
"name": "ingredient 2",
"quantity": "1 tbsp"
}
]
},
{
"id": 2,
"name": "Recipe name2",
"ingredients": [
{
"name": "ingredient 1",
"quantity": "1 tbsp"
}
]
}
]
Now, I'll paste the sample json here on quicktype.
It generates all the required classes for me using the json. Just skip past the code below as it is generated from the site to see the code in action.
// To parse this JSON data, do
//
// final recipe = recipeFromJson(jsonString);
import 'dart:convert';
List<Recipe> recipeFromJson(String str) => List<Recipe>.from(json.decode(str).map((x) => Recipe.fromJson(x)));
String recipeToJson(List<Recipe> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Recipe {
Recipe({
this.id,
this.name,
this.ingredients,
});
int id;
String name;
List<Ingredient> ingredients;
factory Recipe.fromJson(Map<String, dynamic> json) => Recipe(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
ingredients: json["ingredients"] == null ? null : List<Ingredient>.from(json["ingredients"].map((x) => Ingredient.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name == null ? null : name,
"ingredients": ingredients == null ? null : List<dynamic>.from(ingredients.map((x) => x.toJson())),
};
}
class Ingredient {
Ingredient({
this.name,
this.quantity,
});
String name;
String quantity;
factory Ingredient.fromJson(Map<String, dynamic> json) => Ingredient(
name: json["name"] == null ? null : json["name"],
quantity: json["quantity"] == null ? null : json["quantity"],
);
Map<String, dynamic> toJson() => {
"name": name == null ? null : name,
"quantity": quantity == null ? null : quantity,
};
}
Use:
Let's say you are using http package to get the json.
var response = await http.get('your_url_for_json');
var body = response.body;
final recipes = recipeFromJson(body);//the first commented line from the generated code.
Now, you can simply use .
to get all values inside
recipes.first.id
for id of first entry.
recipes.first.ingredients.first.name
for name of ingredient of first entry.
Loops also work
for(var r in recepis){
print(r.id);
}
Upvotes: 1