Home > Enterprise >  How to store the id of document inside same doc in flutter firestore
How to store the id of document inside same doc in flutter firestore

Time:09-21

How to store the id of a document inside the same doc in flutter firestore

enter image description here enter image description here

I am getting the recipe id null

Future updateRecipeData(RecipeModel recipeModel) async {
    await recipeDataCollection.doc().set({
      rRecipeTitle: recipeModel.title,
      rRecipeBundleName: recipeModel.recipeBundleName,
      rCookingTime: recipeModel.cookingTime,
      rDateTime: recipeModel.dateTime,
      rImgPath: recipeModel.imgPath,
      rIngredients: recipeModel.ingredients,
      rNumOfLikes: recipeModel.numOfLikes,
      rPreparation: recipeModel.preparation,
      rServings: recipeModel.servings,
    });
    print('Data TRANSFORMED');
  }

  List<RecipeModel> getRecipes(QuerySnapshot snapshot) {
    return snapshot.docs.map((doc) {
      return RecipeModel(
        title: doc.data()[rRecipeTitle],
        cookingTime: doc.data()[rCookingTime],
        imgPath: doc.data()[rImgPath],
        ingredients: List.from(doc.data()[rIngredients]),
        preparation: List.from(doc.data()[rPreparation]),
        numOfLikes: doc.data()[rNumOfLikes],
        dateTime: doc.data()[rDateTime],
        servings: doc.data()[rServings],
        recipeBundleName: doc.data()[rRecipeBundleName],
        recipeId: doc.id,
      );
    }).toList();
  }

  Stream<List<RecipeModel>> get recipeData {
    return recipeDataCollection.snapshots().map(getRecipes);
  }

This is the code I want to the doc id store it inside recipeid.

RECIPE MODEL CODE



class RecipeModel {
  String title, imgPath, recipeBundleName, recipeId;
  String cookingTime, servings;
  int numOfLikes;
  String dateTime;
  List<String> ingredients, preparation;

  RecipeModel({
    this.title,
    this.cookingTime,
    this.imgPath,
    this.recipeBundleName,
    this.servings,
    this.numOfLikes,
    this.dateTime,
    this.ingredients,
    this.preparation,
    this.recipeId,
  });
}

I shared the recipe model and has recipeId to store the id of the doc inside same doc .

CodePudding user response:

can you share the codes? In order to print the id of the same document, you need to create the id in the codes and send it while setting or updating it.

  • Related