Alternative ways of retrieving documents reference if they do not exist yet

Resolved

Hello Flutterflow community,

I have a small issue with my FlutterFlow project. I have a "Tasks" page which includes a listview, generating all tasks contained in the achievement_detail collection, shared among all users of the app. This task takes the form of a component, in which I pass various parameters (name, time, description...). Each task can be accomplished by users, and by meeting certain conditions, they can complete it. This information is reflected in the sub-collection achievement_status, inside the achievement_detail collection.



As it stands, each task in achievement_detail has a sub-collection achievement_status, with a number of documents theoretically equal to the number of users. To avoid additional costs with Firebase, the status is only created under certain conditions (lazy loading, only if the user clicks on the task component or if the user completes the task). This prevents me from creating a status for each task for each user (which would explode the quotas). The document identifier in achievement_status is the user's uid, to simplify things and keep track.

In the task component, as mentioned above, we find the name, timestamp and description. The problem is that I also need the information from achievement_status when creating the component, since I use the information from its fields for several things (redirection, for example, as shown below using the field accomplish_unlocked contained in achievement_status).

Therefore, achievement_status needs to be retrieved at the same time as the listview elements are generated for everything to function (which is why I'm doing a query on the achievement_card component in the listview. I'm passing the retrieved doc as a parameter). If I try to retrieve inside the component (on an initialization action, trying to read the document with the user's uid as ref), some elements are not appearing.

But I find myself stuck: how to obtain the document from reference of achievement_status (the user's uid), if it does not yet exist? One solution I found is to query the collection and filter on the user's uid, but this would imply too high costs, which I prefer to avoid. Similarly, I had the idea of using a custom function to do a try/catch to return the necessary document, and in case of failure, return a default document. But Flutterflow's custom functions do not allow for async, which blocks everything as well.

I'm not sure if I'm clear in my explanations, so don't hesitate to ask me if needed. Thank you all for your help!

2
3 replies