0

Let's say I have something like this:

class event{

  double timePassed;

  void everySecond(){
    timePassed +=1;
  }
}

I would like the function everySecond to run every second on the firebase server so that User1 and User2 that just logged into the app will see the exact same timePassed value and the function would be able to be run even if the user isn't using the app.

Is it possible to do this using only Google's firebase?

fabriziog
  • 399
  • 6
  • 16
  • 1
    To run code at an interval, it looks like you should use `Timer.periodic()` as shown here https://stackoverflow.com/a/54611581. Then in there, you'd write a value to Firebase with the API, either for [Firestore](https://github.com/FirebaseExtended/flutterfire/blob/master/packages/cloud_firestore/example/lib/main.dart#L66) or the [Realtime Database](https://github.com/FirebaseExtended/flutterfire/blob/master/packages/firebase_database/example/lib/main.dart#L104). – Frank van Puffelen Sep 19 '19 at 22:43
  • @FrankvanPuffelen I'm sorry, but I miswrote the question, I'm trying to make the function everySecond on a firebase server so that users can all get the same timePassed value. – fabriziog Sep 20 '19 at 10:21
  • Counting seconds on the server is going to be expensive, and unreliable. Instead, consider writing the start/end time on the server, and then having each client count down/up individually. – Frank van Puffelen Sep 20 '19 at 10:28

1 Answers1

0
  void abc(){ 
  Timer.periodic((Duration(milliseconds: 1000)),(a)=>{
  //You can put your code here..
}); }
Hardik Kumbhani
  • 2,013
  • 10
  • 18