What is the recommended way to apply CRUD operations to a document in Cloud Firestore using Flutter?









up vote
4
down vote

favorite












I am looking into migrating a large application written in Xamarin and moving to Flutter with Cloud Firestore as the backend. Since Cloud Firestore is still in beta there appears to be very few concrete examples demonstrating best practices when called from a Flutter client.



In particular - there seems to be no direct approach to checking whether a TX has failed or whether it succeeds.



So taking the standard 'Delete' operation as an example - what is the best approach to rolling your own transactions within both online/offline mode including providing appropriate error handling in Flutter ?



I have expanded on my solution below (in the remove method) and you can see that two network calls are being made, one to get the latest document reference and another to perform the delete.



However, since we use a StreamBuilder that gets updates to when any documents change , can we not positively assume that widget.documents[index].reference will hold the latest document reference and therefore do away with :



var snapshot=await tx.get(docReference)
if (snapshot.exists)
await tx.delete(snapshot.reference)



and just use :



 var docReference = widget.documents[index].reference;
await tx.delete(docReference);


Note



Both of the approaches above work - i am after an approach that conforms 100% to ACID test in relation to transactions as well as offering best performance.



Example use case



Streambuilder subscription:



Streambuilder subscription



In the FirestoreListView a ListView.Builder is created that adds delete/edit button that allows any item to be deleted or added , thus we have something like this :



Listview:



Listview



Behind the delete button, we perform the delete as :



Delete code:



Delete code










share|improve this question



















  • 1




    Personally I would do it without the transaction, we already have the document reference. I don't see any critical issues that widget.documents[index].referece.delete() would cause. If the delete goes into problems, the same try catch approach can handle the error.
    – Joshua Chan
    Nov 12 at 6:43














up vote
4
down vote

favorite












I am looking into migrating a large application written in Xamarin and moving to Flutter with Cloud Firestore as the backend. Since Cloud Firestore is still in beta there appears to be very few concrete examples demonstrating best practices when called from a Flutter client.



In particular - there seems to be no direct approach to checking whether a TX has failed or whether it succeeds.



So taking the standard 'Delete' operation as an example - what is the best approach to rolling your own transactions within both online/offline mode including providing appropriate error handling in Flutter ?



I have expanded on my solution below (in the remove method) and you can see that two network calls are being made, one to get the latest document reference and another to perform the delete.



However, since we use a StreamBuilder that gets updates to when any documents change , can we not positively assume that widget.documents[index].reference will hold the latest document reference and therefore do away with :



var snapshot=await tx.get(docReference)
if (snapshot.exists)
await tx.delete(snapshot.reference)



and just use :



 var docReference = widget.documents[index].reference;
await tx.delete(docReference);


Note



Both of the approaches above work - i am after an approach that conforms 100% to ACID test in relation to transactions as well as offering best performance.



Example use case



Streambuilder subscription:



Streambuilder subscription



In the FirestoreListView a ListView.Builder is created that adds delete/edit button that allows any item to be deleted or added , thus we have something like this :



Listview:



Listview



Behind the delete button, we perform the delete as :



Delete code:



Delete code










share|improve this question



















  • 1




    Personally I would do it without the transaction, we already have the document reference. I don't see any critical issues that widget.documents[index].referece.delete() would cause. If the delete goes into problems, the same try catch approach can handle the error.
    – Joshua Chan
    Nov 12 at 6:43












up vote
4
down vote

favorite









up vote
4
down vote

favorite











I am looking into migrating a large application written in Xamarin and moving to Flutter with Cloud Firestore as the backend. Since Cloud Firestore is still in beta there appears to be very few concrete examples demonstrating best practices when called from a Flutter client.



In particular - there seems to be no direct approach to checking whether a TX has failed or whether it succeeds.



So taking the standard 'Delete' operation as an example - what is the best approach to rolling your own transactions within both online/offline mode including providing appropriate error handling in Flutter ?



I have expanded on my solution below (in the remove method) and you can see that two network calls are being made, one to get the latest document reference and another to perform the delete.



However, since we use a StreamBuilder that gets updates to when any documents change , can we not positively assume that widget.documents[index].reference will hold the latest document reference and therefore do away with :



var snapshot=await tx.get(docReference)
if (snapshot.exists)
await tx.delete(snapshot.reference)



and just use :



 var docReference = widget.documents[index].reference;
await tx.delete(docReference);


Note



Both of the approaches above work - i am after an approach that conforms 100% to ACID test in relation to transactions as well as offering best performance.



Example use case



Streambuilder subscription:



Streambuilder subscription



In the FirestoreListView a ListView.Builder is created that adds delete/edit button that allows any item to be deleted or added , thus we have something like this :



Listview:



Listview



Behind the delete button, we perform the delete as :



Delete code:



Delete code










share|improve this question















I am looking into migrating a large application written in Xamarin and moving to Flutter with Cloud Firestore as the backend. Since Cloud Firestore is still in beta there appears to be very few concrete examples demonstrating best practices when called from a Flutter client.



In particular - there seems to be no direct approach to checking whether a TX has failed or whether it succeeds.



So taking the standard 'Delete' operation as an example - what is the best approach to rolling your own transactions within both online/offline mode including providing appropriate error handling in Flutter ?



I have expanded on my solution below (in the remove method) and you can see that two network calls are being made, one to get the latest document reference and another to perform the delete.



However, since we use a StreamBuilder that gets updates to when any documents change , can we not positively assume that widget.documents[index].reference will hold the latest document reference and therefore do away with :



var snapshot=await tx.get(docReference)
if (snapshot.exists)
await tx.delete(snapshot.reference)



and just use :



 var docReference = widget.documents[index].reference;
await tx.delete(docReference);


Note



Both of the approaches above work - i am after an approach that conforms 100% to ACID test in relation to transactions as well as offering best performance.



Example use case



Streambuilder subscription:



Streambuilder subscription



In the FirestoreListView a ListView.Builder is created that adds delete/edit button that allows any item to be deleted or added , thus we have something like this :



Listview:



Listview



Behind the delete button, we perform the delete as :



Delete code:



Delete code







flutter google-cloud-firestore






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 21:33









Ehsan Mohammadi

715313




715313










asked Nov 8 at 20:31









Stella

212




212







  • 1




    Personally I would do it without the transaction, we already have the document reference. I don't see any critical issues that widget.documents[index].referece.delete() would cause. If the delete goes into problems, the same try catch approach can handle the error.
    – Joshua Chan
    Nov 12 at 6:43












  • 1




    Personally I would do it without the transaction, we already have the document reference. I don't see any critical issues that widget.documents[index].referece.delete() would cause. If the delete goes into problems, the same try catch approach can handle the error.
    – Joshua Chan
    Nov 12 at 6:43







1




1




Personally I would do it without the transaction, we already have the document reference. I don't see any critical issues that widget.documents[index].referece.delete() would cause. If the delete goes into problems, the same try catch approach can handle the error.
– Joshua Chan
Nov 12 at 6:43




Personally I would do it without the transaction, we already have the document reference. I don't see any critical issues that widget.documents[index].referece.delete() would cause. If the delete goes into problems, the same try catch approach can handle the error.
– Joshua Chan
Nov 12 at 6:43

















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215702%2fwhat-is-the-recommended-way-to-apply-crud-operations-to-a-document-in-cloud-fire%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes















 

draft saved


draft discarded















































 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53215702%2fwhat-is-the-recommended-way-to-apply-crud-operations-to-a-document-in-cloud-fire%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế