0

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

1 Answer 1

1

You are trying to add values ​​to a list that does not exist.

List<RecipeModel>? recipeList;

when you call:

favoriteRecipeList.recipeList!.add(widget.recipe!);

the recipeList is not initialized.

You can make it empty by default:

List<RecipeModel>? recipeList = [];

Or pass an empty list when instantiating the class:

List<RecipeModel> emptyList = [];
FavoriteRecipeModel favoriteRecipeList = FavoriteRecipeModel(emptyList);
Sign up to request clarification or add additional context in comments.

4 Comments

so I have to do it inside the model and everything will be same inside the recipe_details_screen?
@FahimHoque One way or another, the list variable needs to be initialized (i.e. actually contain a list) before you can add stuff to it. It doesn't really matter where or how you do it as long as you can guarantee the variable isn't null by the time something tries to add to it.
@FahimHoque yes, just make your list empty be default: List<RecipeModel>? recipeList = [];
I tried List<RecipeModel>? recipeList = []; inside model and favoriteRecipeList.recipeList!.add(widget.recipe!); inside the details_screen but still getting the null check operator used on a null value

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.