import 'dart:convert'; import 'package:ai_recipe_generation/util/json_parsing.dart'; import 'package:google_generative_ai/google_generative_ai.dart'; class Recipe { Recipe({ required this.title, required this.id, required this.description, required this.ingredients, required this.instructions, required this.cuisine, required this.allergens, required this.servings, required this.nutritionInformation, this.rating = -1, }); final String id; final String title; final String description; final List ingredients; final List instructions; final String cuisine; final List allergens; final String servings; final Map nutritionInformation; int rating; factory Recipe.fromGeneratedContent(GenerateContentResponse content) { /// failures should be handled when the response is received assert(content.text != null); final validJson = cleanJson(content.text!); final json = jsonDecode(validJson); if (json case { "ingredients": List ingredients, "instructions": List instructions, "title": String title, "id": String id, "cuisine": String cuisine, "description": String description, "servings": String servings, "nutritionInformation": Map nutritionInformation, "allergens": List allergens, }) { return Recipe( id: id, title: title, ingredients: ingredients.map((i) => i.toString()).toList(), instructions: instructions.map((i) => i.toString()).toList(), nutritionInformation: nutritionInformation, allergens: allergens.map((i) => i.toString()).toList(), cuisine: cuisine, servings: servings, description: description); } throw JsonUnsupportedObjectError(json); } Map toFirestore() { return { 'id': id, 'title': title, 'instructions': instructions, 'ingredients': ingredients, 'cuisine': cuisine, 'rating': rating, 'allergens': allergens, 'nutritionInformation': nutritionInformation, 'servings': servings, 'description': description, }; } factory Recipe.fromFirestore(Map data) { if (data case { "ingredients": List ingredients, "instructions": List instructions, "title": String title, "id": String id, "cuisine": String cuisine, "description": String description, "servings": String servings, "nutritionInformation": Map nutritionInformation, "allergens": List allergens, "rating": int rating }) { return Recipe( id: id, title: title, ingredients: ingredients.map((i) => i.toString()).toList(), instructions: instructions.map((i) => i.toString()).toList(), nutritionInformation: nutritionInformation, allergens: allergens.map((i) => i.toString()).toList(), cuisine: cuisine, servings: servings, description: description, rating: rating, ); } throw "Malformed Firestore data"; } }