Flutter Firestore not giving updated data when I add a field to a document from the console - firebase

I'm facing an odd bug in Flutter Firestore 2.5.3 where if I add a new field (customer_type) to a document in the Firebase Console, the app fails to retrieve the newly added field and throws an exception:
CastError (type 'Null' is not a subtype of type 'String' in type cast)
But it isn't null. I added the new field with the value from the Firebase Console. It's there in the document. Turns out Firebase is getting the old document data without the newly added field.
I'm using a StreamProvider from Riverpod. Here's the code:
static Stream<Customer?> _watchCustomer(final ProviderRefBase ref) async* {
final auth = await ref.watch(authProvider.last);
if (auth == null) {
yield null;
} else {
final customerRef = _db.collection('customers').toCustomerRef(auth.uid);
await for (var customer in customerRef.snapshots().map((doc) => doc.data())) yield customer;
}
}
What could be going wrong?
EDIT:
All the names, collection & field are correct and so is their type. If I re-install the app or fully clear app storage, the same exact code will work just fine. However, if I just clear the cache it doesn't work. It impedes my workflow when debugging and I'd like to know why it's happening. For additional reference, here is my "Customer" model:
Customer.fromMap(final String documentID, final Map<String, dynamic> map)
: id = documentID,
name = map['customer_name'] as String,
mobile = map['customer_mobile'] as String,
type = map['customer_type'] as String;
It's set to be non-nullable, because I'm establishing a strict schema. But that shouldn't be a concern, cause it isn't null in the database. I've added customer_type to all my documents in the collection. And none of them have the value set to null.
Which could only mean that the map that I get from Firestore, couldn't find the requisite key. Which means I'm still getting the old data (without the new field) from Firestore. What gives? It should give me the updated data when I clear the cache at least.

Because your code is working properly until you add the new field in Firestore console, it means that the problem is with your new field.
The problem is you either added a field in Firestore spelled differently (case sensitive or typo) from your model (class Customer) or the type is not appropriate (e.g. you try to read an int from a String, or you try to read an array from a map, etc.)
Let me know if this does not help?

What I know so far is that the stream returns an immediate local snapshot of the Document before fetching the updated version from the server. Which makes sense; except that if I clear the cache, then it should go to the server directly and not give me any error - which doesn't happen.
I think Firestore might be storing the local snapshot in more places than just the cache, but this is merely speculation on my end.
Currently, I've patched the issue by doing a simple null check in the constructor:
type = map['customer_type'] as String? ?? 'customer'
This fix is, however, inconclusive and I won't be marking this as the answer as I feel a proper explanation of what's happening is due.

Related

Firebase cross-service Security Rules not working in application

I'm trying to use the new Firebase cross-service Security Rules (https://firebase.blog/posts/2022/09/announcing-cross-service-security-rules) but I having some problems with Storage Rules accessing to Firestore data.
The problem seems to be with userIsCreator() function
match /certification/{certificationId}/{fileId} {
function userIsCreator() {
let certification = firestore.get(/databases/(default)/documents/certifications/$(certificationId));
return firestore.get(certification.data.creatorRef).id == request.auth.uid;
}
allow read, write: if userIsCreator()
}
The content of the Firestore Document is:
{
"data": {
othersValues,
"creatorRef": "/databases/%28default%29/documents/users/CuutSAtFkDX2F9T8hlT4pjMUByS2"
}
"id": "3EhQakDrsKxlacUjdibs"
"__name__":
"/databases/%28default%29/documents/certifications/3EhQakDrsKxlacUjdibs"
}
The creatorRef variable is a reference to a Firestore Document to user. Inside Users collection, the doc id is the UID of an user, so I'm obtaining the creatorRef of an item and then checking if the id of that user collection referenced is the same UID that user logged in.
The same function is working for Firestore Rules to avoid updating certification document if not the creator, without any problem.
It seems to be a problem calling to firestore.get to creatorRef after obtaining it but it not make sense!
Tested:
If I use Firestore Storage Rules validator, it is not failing and it says I have access to that resource from the UID typed in the tester (for other UID is failing as expected). But in my app, even logged in with creator user is getting permission error.
If changing the function to only one call directly to the Users collection id (return firestore.get(/databases/(default)/documents/users/CuutSAtFkDX2F9T8hlT4pjMUByS2).id == request.auth.uid;), it is working in the tester and my app. But it isn't a solution because I need to get first the Users collection ref for the creator!
For original function in the tester It's getting the variables as expected and returning true if simulate the creator UID! But for any reason, in the real app access it is getting unauthorized if making both calls!
Firebaser here!
It looks like you've found a bug in our implementation of cross-service rules. With that said, your example will create two reads against Firestore but it's possible to simplify this to avoid the second read.
Removing the second read
From your post:
return firestore.get(certification.data.creatorRef).id == request.auth.uid;
This line is a bit redundant; the id field is already contained in the certification.data.creatorRef path. Assuming you are indeed using Firestore document references, the format of creatorRef will be /projects/<your-project-id>/databases/(default)/documents/users/<some-user-id>. You can therefore update your function to the following:
function userIsCreator() {
let certification = firestore.get(/databases/(default)/documents/certifications/$(certification));
let creatorRef = certification.data.creatorRef;
// Make sure to replace <your-project-id> with your project's actual ID
return creatorRef ==
/projects/<your-project-id>/databases/(default)/documents/users/$(request.auth.uid);
}
I've tested this out in the emulator and in production and it works as expected. The benefit of doing it this way is you only have to read from Firestore once, plus it works around the bug you've discovered.

Flutter Firebase local change doesn't update listener stream

I'm relying on Firebase Firestore offline capabilities, so I'm not using await on my queries as stated on the Access Data Offline Firebase doc. I'm expecting that when I write something I'll get an immediate reflection on my read stream, however, I'm only getting an update when the server/remote has been updated. Basically:
Update something in the DB. Note, I'm not using await
_db.doc(parentDoc).collection(DocInnerCollection).doc(childDoc).update({
"name": value,
});
I expect my listeners to be updated immediately. Note I've set the includeMetadataChanges to true as stated in the above doc.
_db.doc(parentDoc)
.collection(DocInnerCollection)
.orderBy('start_date', 'desc')
.limitToLast(1)
.snapshots(includeMetadataChanges: true)
.map((snapshot) {
print(snapshot.metadata.isFromCache)
});
However, I get no such update and instead I only get an update when the server has been updated.
You're requesting only one document with .limitToLast(1), yet are not providing a sort order for your query. This essentially means that you'll get a random document from your collection, and the chances of that being the newly updated document are close to zero.
If you want the latest (not just last) document, you need some ordering criteria to determine what latest means. Typically you'd do this by:
Adding a lastUpdated field to your documents, and setting that to firebase.firestore.FieldValue.serverTimestamp().
Ordering your query on that timestamp with orderBy('lastUpdated', 'desc').
And then limiting to the first result with limit(1).

Firestore document update is failing

i have documents in firestore which i am trying to update. These updates are successful for old users but failing for new users. I have listview where user can see all the added documents. They click on Edit and then they are navigated to a detailed document view where they can make changes and save.
Error which i am receiving
[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception:
[cloud_firestore/not-found] Some requested document was not found.
I am passing document id with following from list view to single document edit view;
data: docId
In the edit page, i am loading the user id and doc id which is coming from previous page.
class Edit extends StatefulWidget {
final DataModel data;
Edit({required this.data});
String ui = FirebaseAuth.instance.currentUser!.uid;
#override
_EditState createState() => _EditState();
}
On save button i am updating firestore with following;
ElevatedButton.styleFrom(primary: Colors.redAccent),
onPressed: () async {
DocumentReference edit = FirebaseFirestore
.instance
.collection('edit')
.doc(ui)
.collection('edit')
.doc(widget.data.documentId);
edit.update({
});
The cloud_firestore/not-found error means the document was not found under the specified id. Please make sure the id really exists in your database.
If you are using the local emulator, make sure you are accessing the correct database.
If you are using persistent storage (offline firestore data) it might be that it still has old data or your device didn't connect to the internet and thus doesn't have the current data.
The error message is telling you that the document you build a reference to was not found for update.
There are a few ways to update data to Cloud Firestore:
update - change fields on a document that already exists.
Fails if the document does not exist.
set - overwrite the data in a document, creating the
document if it does not already exist. If you want to only partially
update a document, use SetOptions.merge(). If you want to be able to
create a document if it doesn't exist, or update it if it does
already exist, you should pass SetOptions.merge as the second
parameter, as explained by the API documentation.
So you just need to use the correct operation for your use case.
If you want to update a document without knowing if it exists or not,
you want to use a set() with the merge() option like this (this
example is for Java):
// Update one field, creating the document if it does not already exist.
Map<String, Object> data = new HashMap<>();
data.put("capital", true);
db.collection("cities").document("BJ") .set(data, SetOptions.merge());
Also one classical problem when using auth.currentUser is that it is
possible that the Auth object is not fully initialized and that
auth.currentUser.uid is therefore null. As explained in the doc you
should either use the onAuthStateChanged() observer or check that
auth.currentUser is not null.
Try :
ElevatedButton.styleFrom(primary: Colors.redAccent),
onPressed: ()
async {
DocumentReference edit = FirebaseFirestore .instance .collection('edit') .doc(FirebaseAuth.instance.currentUser.uid) .collection('edit') .doc(widget.data.documentId);
edit.update({ });

Flutter & Firebase: How to populate an array and then later, return all the contents

I have been trying to get arrays working in Firebase, and I am aware that there are a lot of references and discussions about this online, and I have read through all of these and none of it works.
First off, the Firebase side. The structure containing the array and two example strings inside it:
Firebase Structure
collection -> document -> fields
userData profileImages URLs (array)
: https://firebasestorage.googleapis.com/v0/b/app-138804.appspot.com/o/jRwscYWLs1DySLMz7jn5Yo2%2Fprofile%2Fimage_picker4459623138678.jpg?alt=media&token=ec1043b-0120-be3c-8e142417
: https://firebasestorage.googleapis.com/v0/b/app-138804.appspot.com/o/jRwscYWLs3872yhdjn5Yo2%2Fprofile%2Fimage_picker445929873mfd38678.jpg?alt=media&token=ec3213b-0120-be9c-8e112632
The first issue I am facing is writing to this array in the database:
Firestore.instance.collection('userData').document('profileImages').updateData({
'URLs': _uploadedFileURL,
});
Whenever I add data to this array, it just overwrites the existing data. I need to be able to keep all the existing data intact and simply add the current new line to the array.
Once this is working, I then need to be able to return all of the strings in this array without needing to know how many of them there will be.
For this part, I basically have nothing at this point. I could show some of the things I have tried based on suggestions from other articles on this, but none of it is even close to working correctly.
im assuming that _uploadedFileURL is a String, and you are updating the property URLs, that's why your data gets overwritten, because you are changing the URLs value to a single string which is _uploadedFileURL. to solve this issue, simply get the current data inside profileImages before commiting the update. like so
final DocumentSnapshot currentData = await Firestore.instance.collection('userData').document('profileImages').get();
Firestore.instance.collection('userData').document('profileImages').updateData({
'URLs': [
...currentData.data['URLs'],
_uploadedFileURL
],
});
and for the second part of your question, all you need is to query for the profileImages
Future<List<String>> _getProfileImages() {
final document = Firestore.instance.collection('userData').document('profileImages').get();
return document.data['profileImages]
}
the result of the get method will be a DocumentSnapshot, and inside the data property will access the profileImages which is a List<String>.
Ok guys and girls I have worked this out. Part 1: appending data to an array in Firebase.
Firestore.instance.collection('userData').document('profileImages').updateDataupdateData({
'URLs':FieldValue.arrayUnion([_uploadedFileURL]),
});
Where _uploadedFileURL is basically a string, for these purposes. Now I have read that arrayUnion, which is super groovy, is only available in Cloud Firestore, and not the Realtime Database. I use Cloud Firestore so it works for me but if you are having issues this might be why.
Now what is extra groovy about Cloud Firestore is that you can similarly remove an element from the array using:
Firestore.instance.collection('userData').document('profileImages').updateDataupdateData({
'URLs':FieldValue.arrayRemove([_uploadedFileURL]),
});
So how to get this data back out again. A simple way I have found to get that data and chuck it into a local array is like so:
List imageURLlist = [];
DocumentReference document = Firestore.instance.collection('userData').document('profileImages');
DocumentSnapshot snapshot = await document.get();
setState(() {
imageURLlist = snapshot.data['URLs'];
});
From here at least you have the data, can add to it, can remove from it and this can be a platform for you to figure out what you want to do with it.

Correctly updating the same object on create trigger in firebase realtime DB trigger

I have a firebase realtime database trigger on a create node. my need is to update a property based on some condition in the create trigger for the same object. The way i am doing currently is below:
exports.on_order_received_validate_doodle_cash_order = functions.database.ref("/orders/{id}")
.onCreate((change, context) => {
console.log("start of on_order_received_deduct_doodle_cash")
const orderId = context.params.id
const order = change.val();
var db = admin.database();
const orderRef = db.ref('orders/')
return orderRef.child(orderId).update({"_verifiedOrder": true})
})
As you can see i am getting order id from context and then querying object again and updating it. My question is do i need to do this circus or can i just update it without querying again?
Generally it looks good. Just some small feedback to make you feel more confident about being on the right track.
Call the parameter snapshot instead of change because the parameter name change only make sense for the onUpdate event trigger.
You do not need to log that you're entering the function. Because entering and leaving the function is automatically logged by Firebase also.
You can remove the order variable that is unused.
You are actually not "querying" the object again. Making a reference to a node in the database doesn't make any network call itself. Not until you subscribe to receiving data. So doing orderRef.child(orderId) is not a query, it's just a database reference.
You can use the snapshot's own reference attribute to shorten your code a bit... effectively throwing away almost all code :-)
So your code code look like this instead. It is doing the exact same thing, just shorter. It was also correct from the beginning.
exports.on_order_received_validate_doodle_cash_order = functions
.database
.ref("/orders/{id}")
.onCreate((snapshot) => {
return snapshot.ref.child("_verifiedOrder").set(true);
});
But as mentioned in my comment above, you are effectively just setting a flag that is confirming that data was saved (or rather: confirming that the function was triggered). You might want to add some logic in there to check whether the order can be placed or not and then set the verified flag to true or false depending on that. Because with the logic of the implementation, all orders will have the value _verifiedOrder set to true, which is a waste of storage in your database.

Resources