I have developed an app where I get a list of users constantly updated as they enter the vicinity. It shows up fast & fine when there are a few users (roughly less than 100), but takes time when it crosses the mark. Is there a particular way that I can get the list as soon as possible? It has a bunch of criteria where the list gets showed up, to which I am assuming it is taking that much time to load. Please let me know what I can do here to optimise the time and get the list ASAP. Thank you!
Conditions of getting the "make friends" list is as follows:
- The list of all the users who are not in friends with the current user
- Both the users (current user and the other user who are not friends with each other) should be on the same venue they have booked the entry for
- The entry is marked as "1" from the venue side once they enter the venue
- (assuming the user can go to multiple venues on the same day) They should get the list of users, where the last entry is marked as 1
2
Answers
To increase the performance, the best way is to put all data related to the query inside the same collection, in this case, the
user
collection.Also when you are not dealing with duplicated entries, prefer using maps, instead of arrays, because it’s better for Firebase Security Rules and performing queries, so it would be something like:
user
document example:So I think the query would be something similar to this:
How are u doing?
In Flutter, you can use the
shared_preferences
package to store and retrieve small amounts of data persistently.You can also use packages for saving data in cache memory including photos!
Here’s a step-by-step guide on how to get data and save it using the
shared_preferences
package:Add Dependency:
Start by adding the
shared_preferences
package to your Flutter project. You can do this by adding the following dependency to yourpubspec.yaml
file:After adding the dependency, run
flutter pub get
to fetch it.Import the Package:
In your Dart code, import the
shared_preferences
package like this:Save Data:
To save data using
shared_preferences
, you need to create an instance ofSharedPreferences
and then use it to store your data. Here’s an example of how to save a simple key-value pair:You can use
setBool
,setInt
,setDouble
, or other similar methods depending on the type of data you want to store.Retrieve Data:
To retrieve the saved data, you can use the following code:
Again, use
getBool
,getInt
,getDouble
, or corresponding methods if you stored different data types.Error Handling:
Remember to handle potential exceptions when working with
shared_preferences
. For example, if there’s an issue with initializing or writing to storage, you should handle these exceptions gracefully.Usage:
Call the
saveData()
function to save your data when needed, and callfetchData()
to retrieve it. You can use these functions in response to user interactions or at specific points in your app’s lifecycle.Hope this helps, good luck!!