how to stop/dispose timer when offline? - flutter

I called some methods using timer..I used 4 timers. I need to call only when online. how to stop or disable timer when offile?
void connectionChanged(dynamic hasConnection) {
setState(() {
isOffline = !hasConnection;
if(!isOffline){
timer1 = Timer.periodic(Duration(seconds: 1), (Timer t) {
checkRefresh();
checkQuick(_url, tokens);
});
timer3 = Timer.periodic(Duration(seconds: 5), (Timer t) {
globals.getQuick().then((onValue) {
setState(() {
isQuick = onValue;
});
});
});
timer = Timer.periodic(Duration(seconds: 10), (Timer t) {
submitRequestSave(_url, tokens);
});
timer2 = Timer.periodic(Duration(seconds: 10), (Timer t) {
storeSync(_url, tokens);
});
}
});
}
I used dispose when navigate
#override
void dispose() {
timer1.cancel();
timer2.cancel();
timer3.cancel();
super.dispose();
}

Related

How to add continuously change color of container in flutter between RGB values

I want to create an animation where the container changes colors just the LED lights do in real life.
I this would be automatic so I dont want any 'onPressed()' working classes
I tried doing it through 'Timer' and 'AnimatedContainer()' but the timer only responds to initState value i.e. 0
Either help me setState the state of Container after every second or give me some another solution.
This is my code currently
late Timer timer;
int start = 0;
void startTimer() {
const oneSec = Duration(seconds: 1);
timer = Timer.periodic(
oneSec,
(Timer timer) {
setState(() {
start++;
});`your text`
},
);
}
#override
void initState() {
super.initState();
startTimer();
setState(() {
_changeColor();
});
}
_changeColor() {
setState(() {
if (start % 3 == 0) {
changingColor = Colors.green;
} else if (start % 3 == 1) {
changingColor = Colors.blue;
} else if (start % 3 == 2) {
changingColor = Colors.red;
}
});
}
Use setState for start won't call _changeColor, so you have to call _changeColor inside Timer.
class LEDWidget extends StatefulWidget {
const LEDWidget({super.key});
#override
State<LEDWidget> createState() => _LEDWidgetState();
}
class _LEDWidgetState extends State<LEDWidget> {
Color changingColor = Colors.green;
int start = 0;
late Timer timer;
#override
void dispose() {
timer.cancel();
super.dispose();
}
#override
void initState() {
super.initState();
timer = Timer.periodic(const Duration(seconds: 1), (_) {
start++;
_changeColor();
});
}
_changeColor() {
setState(() {
if (start % 3 == 0) {
changingColor = Colors.green;
} else if (start % 3 == 1) {
changingColor = Colors.blue;
} else if (start % 3 == 2) {
changingColor = Colors.red;
}
});
}
#override
Widget build(BuildContext context) {
return AnimatedContainer(
duration: const Duration(seconds: 1),
color: changingColor,
);
}
}

error might indicate a memory leak if setState() is being called because another object is retaining a reference

So for some reason I don't understand the purpose of mounted property. Hope someone could explain it further. Thanks in advance :)
bool get mounted => _element != null;
void getCurrentLocation() async {
locator.Location location = locator.Location();
location.getLocation().then(
(location) {
currentLocation = location;
},
);
GoogleMapController googleMapController = await _controller.future;
location.onLocationChanged.listen(
(newLoc) {
currentLocation = newLoc;
googleMapController.animateCamera(
CameraUpdate.newCameraPosition(
CameraPosition(
zoom: 13.5,
target: LatLng(
newLoc.latitude!,
newLoc.longitude!,
),
),
),
);
if (!mounted) {
return;
}
setState(() {});
// try {
// setState(() {});
// // } catch (e) {
// } on Exception catch (e) {
// _disposeController();
// }
},
);
}

is there a way to run timer in background?

I am able to run timer when the screen is loaded but I want to run that same timer in background also is there a way to do that? and I also want to convert that 30 to 30 minutes need help.
static const maxMinutes = 30;
var minute = maxMinutes;
Timer timer;
void initState() {
// TODO: implement initState
super.initState();
startTimer();
}
void dispose() {
// TODO: implement dispose
super.dispose();
minute;
startTimer();
}
void startTimer(){
timer = Timer.periodic(Duration(seconds: 1), (timer) {
if(mounted) {
setState(() {
if (minute > 0) {
minute--;
} else {
timer.cancel();
}
});
}
});
}
timer is running but i want to run the timer in background

Timer.periodic flutter,

#override
void initState() {
super.initState();
bool isLastPage = (_currentPage.round() == sliderArrayList.length - 1 );
//Trying to build automatic scrollable
Timer.periodic(Duration(seconds: 5), (Timer timer) {
if (_pageController.hasClients && !isLastPage) {
_pageController.nextPage(
duration: Duration(milliseconds: 200),
curve: Curves.easeIn);}
if(isLastPage){
timer.cancel();
}
});
}
I am trying to build a periodic timer which scrolls pages in pageview, flutter after every 5 seconds, however it keeps on scrolling even after the last page. I have tried to implement the above method if(isLastPage){timer.cancel();} and it doesnt work.
You've to evaluate that expression everytime timer ticks. Also, Put the isLastPage check before the hasClients check.
Timer.periodic(Duration(seconds: 5), (Timer timer) {
final isLastPage = _currentPage.round() == sliderArrayList.length - 1;
if (isLastPage) {
timer.cancel();
return;
}
if (_pageController.hasClients) {
_pageController.nextPage(
duration: Duration(milliseconds: 200),
curve: Curves.easeIn);
}
});

Flutter Timer.periodic register/run once

i would like to have a Timer runing once in main.dart to check user activity
#override
void initState() {
super.initState();
initPlatformState();
_fetchMasterData("a", "b");
_startActivityTimer();
}
bool _activityTimerRunning = false;
void _startActivityTimer() {
if (!_activityTimerRunning) {
Timer.periodic(Duration(seconds: 5), (timer) {
_timerTicked(timer);
setState(() {
_activityTimerRunning = true;
});
print("Timer started");
});
}
}
But initState gets called more than once, so it is not the right place to register the timer. Where should it be placed?
The code in the question has i silly mistake, the print statement is in the wrong place. Here is the working code:
#override
void initState() {
super.initState();
initPlatformState();
_fetchMasterData("a", "b");
_startActivityTimer();
}
void _startActivityTimer() {
print("Timer started");
Timer.periodic(Duration(seconds: 5), (timer) {
_timerTicked(timer);
});
}
"TimerStarted" get executed only once
Try this sample code.
int start = 5;
fName(){
Timer _timer;
const oneSecond = Duration(seconds: 5);
_timer = Timer.periodic(
oneSecond,
(Timer timer) => setState((){
if(start == 0)
timer.cancel();
else{
_activityTimerRunning = true;
start -= 1;
}
})
);
}
When you want to start you timer, call fName() method, after that timer can started. So and also you can check condition, when
if(start != 0){
...
}