fix: improve JSON parsing in Recipe.fromJson for better type handling
This commit is contained in:
@@ -13,11 +13,23 @@ class Recipe {
|
|||||||
this.servings,
|
this.servings,
|
||||||
});
|
});
|
||||||
|
|
||||||
factory Recipe.fromJson(Map<String, dynamic> json) => Recipe(
|
factory Recipe.fromJson(Map<String, dynamic> json) {
|
||||||
id: json['id'] as int,
|
final dynamic rawId = json['id'];
|
||||||
title: json['title'] as String,
|
final dynamic rawTitle = json['title'] ?? json['name'];
|
||||||
description: json['description'] as String?,
|
final dynamic rawDescription = json['description'];
|
||||||
imageUrl: json['imageUrl'] as String?,
|
final dynamic rawImageUrl = json['imageUrl'];
|
||||||
servings: json['servings'] as int?,
|
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())),
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user