Home > database >  how to add update and delete firestore in flutter?
how to add update and delete firestore in flutter?

Time:05-21

I created an icon where I want to create an update function for the database in firestore, I want to enter the contents of the update myself (using a textfield) can you give a simple example?

CodePudding user response:

here for delete method and Edit method

void delete(dynamic height, dynamic weight, String email) async {
    var androidAds = (await FirebaseFirestore.instance
            .collection('userdata')
            .doc(email.trim())
            .get())
        .data();

    List lstUser = androidAds!["data"];

    List newArray = [];
    for (var index = 0; index < lstUser.length; index  ) {
      Map<String, dynamic> element = lstUser[index];

      if (element["Height"] != height.toString() &&
          element["Weight"] != weight.toString()) {
        Map<String, dynamic> mapInsert = {
          "Height": element["Height"],
          "Weight": element["Weight"],
        };
        newArray.add(mapInsert);
      }
    }
    await FirebaseFirestore.instance
        .collection('userdata')
        .doc(email.trim())
        .set(
      {
        "data": newArray,
      },
    );
    setState(() {});
  }



 // here for Edit method

  void edit(
    dynamic height,
    dynamic weight,
    String email,
    String heightController,
    String weightController,
  ) async {
    var androidAds = (await FirebaseFirestore.instance
            .collection('userdata')
            .doc(email.trim())
            .get())
        .data();

    List lstUser = androidAds!["data"];

    List newArray = [];
    for (var index = 0; index < lstUser.length; index  ) {
      Map<String, dynamic> element = lstUser[index];

      if (element["Height"] == height.toString() &&
          element["Weight"] == weight.toString()) {
        Map<String, dynamic> mapInsert = {
          "Height": weightController,
          "Weight": heightController,
        };
        newArray.add(mapInsert);
      } else {
        Map<String, dynamic> mapInsert = {
          "Height": element["Height"],
          "Weight": element["Weight"],
        };
        newArray.add(mapInsert);
      }
    }
    await FirebaseFirestore.instance
        .collection('userdata')
        .doc(email.trim())
        .set(
      {
        "data": newArray,
      },
    );
    setState(() {});
  }
  • Related