Flutter + Firebase: What to do where and why

98 views
Skip to first unread message

Sohaib Ahmed

unread,
Jan 3, 2022, 12:22:20 PM1/3/22
to Firebase Google Group
Hi, :)

I remember Todd saying that a good structure for Firebase is to store the same data in multiple places for fast reads and using cloud functions to update those data everywhere for writes. My question then becomes what actions should be taken in the "frontend" like Flutter, Dart code and in the "backend" like firebase cloud functions. Let's take these two scenarios

1) User updated profile image. I assume I should use the frontend to update the main place for that profile image reference in something like a "users" collection. And any other place that image occurs, like chat previews should be taken care of by cloud functions. If thats the case why should it be this way? I could just update everyplace with flutter/dart code and since the firebase calls would be async there is nothing blocking the changes for the user, and I assume it wouldn't be any slower than cloud functions?

2) I think u guys might give a good answers for the first scenario, then my question becomes. If I make a chat application and structure the data something like [collection: users] -> [user doc] -> [collection: chats] -> [recipient doc] -> [collection: messages] -> [message doc]. Here i will send a message and store that for both users, I will also have some information on the recipient doc like last message, time and so on. These docs will also need updates when a new message is sent. Which actions (1. store message for me, 2. store message for the receiver, 3. update the recipient doc for me, 4. update the recipient doc for the receiver) should then be done in flutter/dart code and which in cloud function and does it make a difference?

Let me know if I should explain something further or add more details
Best regards, Sohaib


Saad Attieh

unread,
Jan 3, 2022, 4:10:30 PM1/3/22
to fireba...@googlegroups.com
Apologies, typing this on my phone so may have to be a little brief.

Answering your first question, you can in some cases do everything in the front end, though you may consider putting all updates within a transaction or a batch right to ensure that either all the updates are made successfully or none of them. However it is good practice to do it using some sort of back end/cloud function, updating loads of documents for such a small change on the front end can consume too much resource especially network. 

In your specific scenario, you might not need to even store the reference. If you said it such that user profile images are always stored at A path such as /public/<user_id>_profile, then anyone who knows the user ID can retrieve the profile picture. 

With regards to your second question, I would suggest a top level collection called messages which has as fields send_ref recipient_ref, date, content. Here you don’t need to do any copying, you can easily filter all messages sent by a particular person or all messages received by a particular person and you can even order it by date. It is then very easy to have a real time listener that shows the most recent messages.


 Sent from my iPhone (excuse typos)

On 3 Jan 2022, at 8:22 pm, Sohaib Ahmed <sohaib.no...@gmail.com> wrote:

Hi, :)
--
You received this message because you are subscribed to the Google Groups "Firebase Google Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/460b67c8-9a17-45c2-bf00-e48bd222d9d5n%40googlegroups.com.

Sohaib Ahmed

unread,
Jan 4, 2022, 11:53:01 AM1/4/22
to fireba...@googlegroups.com
Hi, Saad thanks for your explanation :)

1) I see your point, I should not use too much resources on the frontend i.e. the users device for mye backend maintenance. For your second point not sure if I understood it correctly, yes I do have a users collection where the user data is stored in documents with the user id as id. This document can be a read by other users like if a load a profile page. I think the point with saving the same data multiple places was that, then I dont need to load that document every time I need to show the users profile picture or name, but store those values on other documents as well as the chat preview, to only make a single call to firebase instead of multiple. Let me know if I misunderstood your point.

2) I agree with you that it's probably better for me not to duplicate the data. I will try out this solution, thanks for the help!

You received this message because you are subscribed to a topic in the Google Groups "Firebase Google Group" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/firebase-talk/cQYdSDCNBCQ/unsubscribe.
To unsubscribe from this group and all its topics, send an email to firebase-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/firebase-talk/450D8DC0-63D7-4D8B-8DE8-20AFA291DDA9%40asomas.ai.

Tiavina jean fabrice Ranoeliarson

unread,
Jan 9, 2022, 4:44:32 PM1/9/22
to fireba...@googlegroups.com
Fbxd

Reply all
Reply to author
Forward
0 new messages