Countdown Timer That Begins From a Timestamp

Viewed 208

I'm trying to link a countdown timer in Dart with my Firebase database, the countdown starts (timestamp) when the post is published and ends 10 seconds later.

My Dart code works, when I post to the database, my countdown is linked to the post, and the post is removed from the database onEnd, however, the timer is not linked to the post, and every refresh of the page restarts.

This code illustrates the working conditions described in the previous paragraph.

TweenAnimationBuilder<Duration>(
                    duration: Duration(seconds: 10),
                    tween: Tween(begin: Duration(seconds: 10), end: Duration.zero),
                    onEnd: () {
                      deletePost(
                          widget.snap['postId']
                              .toString(),
                      );

I want Begin to be the post (timestamp) in the Firebase database, so I tried something like this. 'datePublished' is the time the post was created in Firebase.

TweenAnimationBuilder<Duration>(
                        duration: Duration(seconds: 10),
                        tween: Tween(begin: widget.snap['datePublished'], end: Duration.zero),
                        onEnd: () {
                          deletePost(
                              widget.snap['postId']
                                  .toString(),
                          );

and it returns

Expected a value of type 'Duration?', but got one of type 'Timestamp'

Firebase does not have a type 'Duration' from what I see so is there another way to do this?

Update

Per some suggestions and as a way to create a link to the back-end (Firebase) I created a Firestore method 'timer' with a integer value of secondsPerMinute and passed it like this:

TweenAnimationBuilder<Duration>(
                   duration: Duration(seconds: widget.snap['timer']),
                   tween: Tween(begin: Duration(seconds: 10), end: Duration.zero),
                   onEnd: () {
                     deletePost(
                         widget.snap['postId']
                             .toString(),
                     );

This creates a correct output in relation to the back-end however, the front-end and back-end are still not "attached" so to speak (every time the page is refreshed the countdown starts over from 10 seconds) my goal is to create a permanent relationship between the creation of the post and the post being deleted after 10 seconds no matter if the user navigates away from the page or closes the page/application all together.

1 Answers

Firebase has no futures, like deleting documents after some period of time. But there is, always, a way you can achieve it.

  1. The best way is to run a server with will track end delete documents that need to be deleted.
  2. You can delete documents using user computer to trigger this event. To do that create a document with, for example, datePublished field using firebase serverTimestamp() because of this timestamp will appear when document will be saved in Firestore you need to right before document was saved download it takes that timestamp and create a new timestamp by adding offset to it and save in another field for example expirationDate when document meant to be deleted. Init a countdown function with will delete document. To avoid that, countdown will be turned off by exiting application or by refreshing application create event that will always trigger on refreshes or opening application and only when user is a creator of a document to trigger countdown again.
  3. Do everything like in 2. example, but don't trigger any countdowns. If user can't read data, data for him not exist. Every time, make requests with query that won't get documents out of date and secure database using rules if it's necessary that user can't get this data. Once a while like for example once a week trigger a firebase function with will delete all out of date documents and that's it.

The second solution is bad in my opinion. It will trigger many failed queries.

Related