How to don't save equals String in Firebase firestore for Flutter

I'm reading a QR Code in my app and save it in firebase, but I need to save it just one time, if it is different save, but if it is equal there is no need to save.

this is my code

await Firestore.instance
       .collection("lockers")
       .document()
       .setData({"numero_serie": _numeroSerie});
}

myapp

Upvotes: 0

Views: 708

Answers (2)

The correct Answer

final QuerySnapshot result = await Future.value(Firestore.instance
    .collection("lockers")
    .where("numero_serie", isEqualTo: "$_numeroSerie")
    .limit(1)
    .getDocuments());

final List<DocumentSnapshot> documents = result.documents;
    if (documents.length == 1) {
    confirmacao(context);
    } else {
    await Firestore.instance
     .collection("lockers")
     .document()
     .setData({"numero_serie": _numeroSerie});
}
}

Upvotes: 0

baradwaj
baradwaj

Reputation: 266

A normal setData will insert value irrespective of what the value of your QR code is. You need to perform a get and check if the value is already present and then insert the data.

Check https://pub.dev/packages/cloud_firestore for reading data and then setting it in Firestore.

Upvotes: 1

Related Questions