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

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()}');
});

Related

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

Flutteronic method of scheduling code to run on the next event loop

This is common in other languages. setTimeout(fn, 0) in JavaScript, and DispatchQueue.main.async() {} in Swift.
How best to do this in Flutter?
I have used Future.delayed(Duration.zero).then(fn), but I don't like it because like JS's setTimeout and unlike swifts DispatchQueue.main.async() {} it doesn't really express the intent, only the behaviour. Is there a way of doing this that is the correct way to do this in Flutter.
Use addPostFrameCallback
WidgetsBinding.instance
.addPostFrameCallback((timestamp) {
print("I'm running after the frame was built");
});
This will cause your callback function to run right after flutter has finished building the current frame.
Note that the callback will only run once, if you want to reschedule it for each build, set the callback at the beginning of the build function.
#override
Widget build(BuildContext context) {
WidgetsBinding.instance
.addPostFrameCallback((timestamp) {
print("I'm running after the frame was built");
});
return Container();
}
You can also use Timer from flutter.
Example
Timer(Duration(seconds: 1), () {
print('hai');
});
Duration gives you options with seconds,milliseconds,days,hours,minutes.
You can achieve setInterval also using Timer
Timer.periodic(Duration(seconds: 1), (Timer timer) {
print('hai');
});
But keep in mind that to cancel the timer on dispose.This would save you from hitting memory
Timer timer;
timer = Timer(Duration(seconds: 1), () {
print('hai');
});
void dispose() {
timer.cancel();
}

How to run a timer in initState flutter?

I've created an app using flutter and I want to get start a timer when the user navigates to a screen to calculate how long will he stay there.
I want if he has completed three minutes on the screen to print anything.
I don't have any ideas about the timer or how to use it.
please help me regarding achieving that.
thanks all.
You can do something like that.
Timer _timer;
#override
void initState() {
_timer = Timer(Duration(milliseconds: 500), () {
// SOMETHING
});
super.initState();
}
#override
void dispose() {
if (_timer != null) {
_timer.cancel();
_timer = null;
}
super.dispose();
}
Please have a look of this:
import 'dart:async';
main() {
const twentyMillis = const Duration(milliseconds:20);
new Timer(twentyMillis, () => print('hi!'));
}
Also look Timer class link

Is it possible to recall a function every x seconds at 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();
}

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
});
...
}