I have a model called RecipeModel which basically has details about recipes(recipeName, recipeAuthor etc).
Now I want the users to mark any recipe as favorite which will basically be a List of RecipeModel.
Here's my FavoriteRecipeModel:
class FavoriteRecipeModel {
List<RecipeModel>? recipeList;
FavoriteRecipeModel({this.recipeList});
factory FavoriteRecipeModel.fromJson(Map<String, dynamic> json) =>
FavoriteRecipeModel(
recipeList: json["recipeList"] == null ? null : json["recipeList"],
);
Map<String, dynamic> toJson() => {
"recipe": recipeList == null ? null : recipeList,
};
}
And here's how I intend to use it in my recipe_details_screen:
class RecipeDetailsScreen extends StatefulWidget {
final RecipeModel? recipe;
RecipeDetailsScreen({required this.recipe});
@override
_RecipeDetailsScreenState createState() => _RecipeDetailsScreenState();
}
class _RecipeDetailsScreenState extends State<RecipeDetailsScreen> {
FavoriteRecipeModel favoriteRecipeList = FavoriteRecipeModel();
void addToFavorites() {
favoriteRecipeList.recipeList!.add(widget.recipe!);
}
......some other widgets
}
problem is whenever I'm trying to call the addToFavorites method I'm getting this error:
Null check operator used on a null value
My plan is to add the recipeModel that has been passed to this page to a list and then save the list in local storage using Hive in a json file.
But I don't understand how to solve the error.
Update 1:
I changed my model to
List<RecipeModel>? recipeList = [];
and inside my details_screen:
FavoriteRecipeModel favoriteRecipeList = FavoriteRecipeModel(); favoriteRecipeList.recipeList!.add(widget.recipe!);
But still getting the null check error