When switching to another page, a periodic timer is started in initState (). But it is duplicated.
Or I am making a wrong transition to another page. Or initState () is doubled.
Code for moving from one page to another:
Navigator.push(
context,
new MaterialPageRoute(
builder: (context) => new MapPage(phone: _phone)));
Timer call:
#override
void initState() {
super.initState();
_timerSendPointStart = Timer.periodic(Duration(seconds: 10), (timer) {
setState(() {
makePoint();
});
});
}
Can you please tell me where the error is and how to fix it?
use onWillPop() life cycle hook to stop the timer/delete the timer before navigating to another screen. otherwise, you will get the timer's overflow.
It's also good to cancel the timer in the dispose() function.
Related
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()}');
});
I am new to flutter and I have a problem with a flutter function. It adds data to graph and I programmed a button to start updating the graph. How do I stop it? I created another button and I want to program it to stop the function that adds the graph.
The function:
void updateDataSource(Timer timer) {
chartData.add(LiveData(time++, (y)));
chartData.removeAt(0);
_chartSeriesController.updateDataSource(
addedDataIndex: chartData.length - 1, removedDataIndex: 0);
}
The first button:
onPressed: () {
Timer.periodic(const Duration(seconds: 1), updateDataSource);
},
The second button:
onPressed: () {
chartData.clear();
},
I put chartData.clear() but that clears everything and I am unable to call back the function again.
Thanks in advance!
This is due to your Timer.periodic function which is running every second
Solution:
Create a state timer variable
Timer? _timer;
Assign a value to the timer when you first onPressed
onPressed: (){
_timer = Timer.periodic(const Duration(seconds: 1), updateDataSource);
setState((){});
},
And when you want to stop adding graphs Do.
onPressed: () {
if(_timer !=null) {
_timer.cancel();
}
},
I have a quiz page which has a button 'end quiz', which navigates to the new page when the user taps on it. It also sends info like questions attempted and options selected to the new page.
I want to have a timer which automatically ends the quiz and does all that the button did.
I have got an idea on how to navigate to a new page after a delay using this:
import 'dart:async';
Timer(Duration(seconds: 5), () {
// 5 seconds over, navigate to Page2.
Navigator.push(context, MaterialPageRoute(builder: (_) => Screen2()));
});
link to where I got this info from
Now where I exactly do I place this so as to also get all the data from the widgets the user interacts with?
From your init Function Exit Use This :
#override
void initState() {
Timer(Duration(seconds: 5), () {
// 5 seconds over, navigate to Page2.
Navigator.push(context, MaterialPageRoute(builder: (_) => Screen2(arguments: { send your data here ..... })));
});
)
Dont Forget to Use Stateful Widget
timer() async {
await Future.delayed(Duration(milliseconds: 5000));
//enter you code to save the data here here
Navigator.push(context, MaterialPageRoute(builder: (context) => child));
}
I am using flutter_bloc and I am not sure how to yield state from within a callback.
Am trying to start timer and return a new state after a pause. Reason I am using a timer is to get the ability to cancel previous timer so it always returns a new state after an idle state.
#override
Stream<VerseState> mapEventToState(
VerseEvent event,
) async* {
if (event is ABCEvent) {
Timer(const Duration(seconds: 3), () {
print("Here");
_onTimerEnd(); // adding yield here returns an error.
})
}
Stream<XYZState> _onTimerEnd() async* {
print("Yielding a state");
yield myNewState();
}
I can see that the code is getting inside the timer callback as I can see the print statements in the callback but not in the timerEnd() method.
State should be yielded by the stream used in bloc that you are current working on. Like
mapEventToState
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();
}