Is it possible to recall a function every x seconds at flutter? - flutter

Is it possible to recall a function every x seconds at flutter? And notify if there are changes?
the code is as follows
List<Any> any =
await Api.getAny(++widget.appState.lastPage, widget.datauser);
widget.appState.issues.addAll(issues);
setState(() {});
}```

Actually It is easy to run function every x amount of seconds on flatter.
You can use Timer to recall function per x seconds.
Timer timer;
void initState() {
super.initState();
timer = Timer.periodic(Duration(seconds: 15), (Timer t) => checkForNewSharedLists());
}
void dispose() {
timer.cancel();
super.dispose();
}

Related

How to call any method or page after every 1 second?

Whenever my home page was reloaded my data was updated but I want to reflect data instantly.
That's why I need a method that can refresh my page or method after every 1 second(flutter).
pls, help me.
You can use the Timer.periodic to run a method every Duration you specify and using StatefulWidgetn you can run it in initState like this:
#override
void initState() {
Timer.periodic(Duration(seconds: 1), (timer) {
print("this will execute every second");
});
super.initState();
}
change the print with your method.
in Your init state call the Timer like this
#override
void initState() {
new Timer.periodic(Duration(seconds: 1), (Timer t) => YourMethod());//api call
super.initState();
}
this will update your full screen
This work's for me :smile:
Timer.periodic(Duration(milliseconds: 2200),
(timer) {
'Your Method'
debugPrint('Timer 1 sec ${timer.tick.toString()}');
});

setState() or markNeedsBuild() called during build. Trying to make a simple timer in flutter

I am trying to make a simple timer which run till a given time. This is how I have tried to call the timer function. It gives the error as mentioned in the title. I believe the error is there because I am calling set state method in the init state, but I really need to make this functionality that, when this widget enters the screen, a timer begins and do something when the timer ends. Any help is greatly appreciated.
late double timeRemaining;
late Timer _timer;
void startTimer(double timeRemaing) {}
#override
void initState() {
timeRemaining =
widget.startDate.difference(widget.endDate).inSeconds / 1000 - 80;
const Duration seconds = Duration(seconds: 1);
_timer = Timer.periodic(seconds, (timer) {
setState(() {
timeRemaining--;
if (timeRemaining <= 0) {
// done = true;
done = true;
timer.cancel();
}
});
});
super.initState();
}
as the title says, you're building widget during another build (when you call setState in the timer).So the solution is to wait for the widget to finish building, then start your timer, this can be done by using addPostFrameCallback, like the following:
#override
void initState() {
timeRemaining =
widget.startDate.difference(widget.endDate).inSeconds / 1000 - 80;
const Duration seconds = Duration(seconds: 1);
// this will schedule a callback for the end of this frame.
WidgetsBinding.instance.addPostFrameCallback((_) {
_timer = Timer.periodic(seconds, (timer) {
setState(() {
timeRemaining--;
if (timeRemaining <= 0) {
// done = true;
done = true;
timer.cancel();
}
});
});
});
super.initState();
}
try it and tell me if this works

Flutter: How do I show a clock that includes milliseconds?

I'm building an app with Flutter and I need to show a clock that includes milliseconds
Here's what I have so far. It works, but only shows seconds.
#override
void initState() {
_timeString = DateFormat('hh:mm:ss').format(DateTime.now());
Timer.periodic(Duration(milliseconds: 50), (Timer t) => _getTime());
super.initState();
}
void _getTime() {
final String formattedDateTime =
DateFormat('hh:mm:ss').format(DateTime.now());
setState(() {
_timeString = formattedDateTime;
});
}
If anyone is interested, using 'hh:mm:ss:SS' is enough.

Is there a way I can continuously change some text in certain interval of time?

In some area, I want a list of objects to be continuously appeared replacing the previous one in a gap of 2 secs. And in that interval I wanna do some logic.
I tried Flutter.delayed but it doesn't work accordingly in a while loop.
In your initState method, use Timer.periodic(...)
#override
void initState() {
super.initState();
// this code runs after every 2 seconds.
Timer.periodic(Duration(seconds: 2), (timer) {
if (_someCondition) {
timer.cancel(); // if you want to stop this loop use cancel
}
setState(() {
_string = "new value"; // your logic here
});
});
}
Create a timer and put your logic in the function that handles the timer event.
...
...
initstate() {
Timer.periodic( Duration(seconds: 2), (Timer t) {
setState(() => displayTheNextElementOfTheList());
...
//your logic here
});
...
}

Async/Await not working as expect. How can I start a timer exactly 4 seconds after a sound has started playing in Flutter?

When my app's game screen appears, an audio file starts playing that says "Ready? 3...2...1...GO" and then plays music.
I'd like the game timer to start at the same time that it says "GO", which is exactly 4 seconds into the audio.
I got pretty close by starting the audio file in initState() and then using "Future.delayed(const Duration(seconds: 4), () => startTimer());" but the problem is that the first time a user plays, it take a bit of time for the audio to load, so then the timer starts before the audio says "GO".
I tried to solve this by making an startGameSequence() function that uses async and await, but it isn't working. What am I doing wrong?
Here's what I've got so far:
import 'package:audioplayers/audio_cache.dart';
import 'dart:async';
class GameScreen extends StatefulWidget {
#override
_GameScreenState createState() => _GameScreenState();
}
class _GameScreenState extends State<GameScreen> {
void loadAudio() {
final player = AudioCache();
player.load('audio/ready-321-go-music.mp3');
}
void playAudio() {
final player = AudioCache();
player.play('audio/ready-321-go-music.mp3');
}
void startGameSequence() async {
await loadAudio();
playAudio();
// After audio finishes loading / starts playing, THEN I'd like the 4-second delayed timer to start. (Currently, it seems that the time starts too early because the audio takes a bit to load.)
Future.delayed(
const Duration(seconds: 4),
() => startTimer(),
);
}
Timer _timer;
double _timeRemaining = 7.00;
void startTimer() {
const tick = const Duration(milliseconds: 10);
_timer = new Timer.periodic(
tick,
(Timer timer) => setState(
() {
if (_timeRemaining < 0.01) {
timer.cancel();
} else {
_timeRemaining = _timeRemaining - 0.01;
}
},
),
);
}
#override
initState() {
super.initState();
startGameSequence();
}
Thank you in advance for any help!
InitState is not async, wherefore this not working correctly.
A one solution is, load the audioFile in initState() and execute startGameSequence() in didUpdateWidget() function without the Future.delayed() .
#override
initState() {
super.initState();
await loadAudio();
}
#override
void didUpdateWidget(GameScreen oldWidget) {
startGameSequence()
super.didUpdateWidget(oldWidget);
}
void startGameSequence() {
playAudio();
}
This function execute just when the first layout appears on the screen.