skip to Main Content

So I’m making an application where each time the user touch the screen the counter is incremented by one this is how it is incremented in the code below.
is there a better way to increment the counter while saving it in the database without sending so many requests but also making sure that it will be the same if the user closed or signed out etc..

void _incrementCounter(){
setState(() {
  count++;
});
FirebaseFirestore.instance.collection('user').doc(uId).update({'count': FieldValue.increment(1)});

2

Answers


  1. If you want to have up-to-date data, then I would do that after each increment, yes.

    But you could also store the number of clicks in the shared preferences and send this value every x seconds/minutes/hours.

    Login or Signup to reply.
  2. In my opinion, making requests per touch is not efficient.¯(ツ)
    Instead, you should store it in a variable and then add it to the firebase value after a particular time interval.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search