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:
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:
Behind the delete button, we perform the delete as :
Delete code:
flutter google-cloud-firestore
add a comment |
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:
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:
Behind the delete button, we perform the delete as :
Delete code:
flutter google-cloud-firestore
1
Personally I would do it without the transaction, we already have the document reference. I don't see any critical issues thatwidget.documents[index].referece.delete()
would cause. If the delete goes into problems, the sametry
catch
approach can handle the error.
– Joshua Chan
Nov 12 at 6:43
add a comment |
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:
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:
Behind the delete button, we perform the delete as :
Delete code:
flutter google-cloud-firestore
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:
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:
Behind the delete button, we perform the delete as :
Delete code:
flutter google-cloud-firestore
flutter google-cloud-firestore
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 thatwidget.documents[index].referece.delete()
would cause. If the delete goes into problems, the sametry
catch
approach can handle the error.
– Joshua Chan
Nov 12 at 6:43
add a comment |
1
Personally I would do it without the transaction, we already have the document reference. I don't see any critical issues thatwidget.documents[index].referece.delete()
would cause. If the delete goes into problems, the sametry
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
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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 sametry
catch
approach can handle the error.– Joshua Chan
Nov 12 at 6:43