From fa06ba09156f0f3b5bc385ca42bd836d551365c7 Mon Sep 17 00:00:00 2001 From: Nils-Johan Gynther Date: Tue, 21 Apr 2026 22:21:08 +0200 Subject: [PATCH] fix: improve JSON parsing in Recipe.fromJson for better type handling --- .../lib/features/recipes/domain/recipe.dart | 26 ++++++++++++++----- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/flutter/lib/features/recipes/domain/recipe.dart b/flutter/lib/features/recipes/domain/recipe.dart index 65b314c8..cef753af 100644 --- a/flutter/lib/features/recipes/domain/recipe.dart +++ b/flutter/lib/features/recipes/domain/recipe.dart @@ -13,11 +13,23 @@ class Recipe { this.servings, }); - factory Recipe.fromJson(Map json) => Recipe( - id: json['id'] as int, - title: json['title'] as String, - description: json['description'] as String?, - imageUrl: json['imageUrl'] as String?, - servings: json['servings'] as int?, - ); + factory Recipe.fromJson(Map json) { + final dynamic rawId = json['id']; + final dynamic rawTitle = json['title'] ?? json['name']; + final dynamic rawDescription = json['description']; + final dynamic rawImageUrl = json['imageUrl']; + final dynamic rawServings = json['servings']; + + return Recipe( + id: rawId is num ? rawId.toInt() : int.parse(rawId.toString()), + title: (rawTitle ?? '').toString(), + description: rawDescription == null ? null : rawDescription.toString(), + imageUrl: rawImageUrl == null ? null : rawImageUrl.toString(), + servings: rawServings == null + ? null + : (rawServings is num + ? rawServings.toInt() + : int.tryParse(rawServings.toString())), + ); + } }