I am using timer.periodic to call some functions at different times. The problem which I am facing is that the timer is much slower than real life like for example what you will see in my code that the timer should finish in 5 seconds but in real life its taking 25 seconds to finish.
void startTheTimer(){
var counter = 5;
final zeroDurationTimer = Timer.run(() {
_StartDataCollection();
});
Timer.periodic(const Duration(seconds: 5), (timer) {
print(timer.tick);
counter--;
if (counter == 2) {
_StopDataCollection();
}else if (counter == 1){
createUser();
}
if (counter == 0) {
print('Cancel timer');
timer.cancel();
print(numbers.length);
print(fifo.length);
}
});
}
the print on the compiler shows the timer ticks as 1-2-3-4-5 but its taking it too long to print 2 and then same goes for the rest of the ticks.
Anyone knows what is going on?
Timer.periodic(const Duration(seconds: 5), (timer) { //do function } mean, it spent 5s to do this function
So, if you do this function 5 times, it will spent 25s
Change to 1s will work:
Timer.periodic(const Duration(seconds: 1), (timer) {
print(timer.tick);
counter--;
if (counter == 2) {
_StopDataCollection();
}else if (counter == 1){
createUser();
}
if (counter == 0) {
print('Cancel timer');
timer.cancel();
print(numbers.length);
print(fifo.length);
}
});
or use for-loop instead timer
for(int i = 0; i < 5; i++){
await Future.delayed(Duration(seconds: 1));
counter--;
print(counter);
if (counter == 2) {
_StopDataCollection();
}else if (counter == 1){
createUser();
}
if (counter == 0) {
print('Cancel timer');
print(numbers.length);
print(fifo.length);
}
}
Related
I have cards which are getting scrolled successfully through a Page Controller and animate to Page. Now my issue is that even though it is iterated correctly, if I scroll myself to the right or left it leaves me back at the starting position aka _currentPage=0. I want the system to know dynamically that if 0 is the starting position and I have scrolled manually to the 3rd position, I want it to go to the 4th position and not start at the 0th position again. Similarly, if I have scrolled backwards to the 6th position I want it to go forward from where I scrolled not back to 0. Is there a way to achieve this dynamically?
Please refer to the Timer.periodic code I tried adding a gesture Detector but that wouldn't work the program would not scroll at all.
Timer _mytimer;
bool _mytimerz = false;
int _timerspeed = 15;
int _currentPage = 0;
bool _speedtime = false;
PageController _pageController = PageController(
initialPage: 0,
);
// timerforthisThis() {
void initState() {
super.initState();
tests = [
widget.label00,
widget.label0,
widget.label1,
widget.label2,
widget.label3,
widget.label4,
widget.label5,
widget.label6,
widget.label7,
widget.label8,
widget.label9,
widget.label10,
widget.label11,
widget.label12,
widget.label13,
widget.label14,
widget.label15,
widget.label16,
widget.label17,
widget.label18,
widget.label19,
widget.label20,
widget.label21,
widget.label22,
widget.label23,
widget.label24,
widget.label25,
widget.label26,
widget.label27,
widget.label28,
widget.label29,
widget.label30,
widget.label31,
widget.label32,
widget.label33,
widget.label34,
widget.label35,
widget.label36,
widget.label37,
];
results = [];
for (int i = 0; i < tests.length; i++) {
if (widget.label00 != '') {
results.add(widget.label00);
}
if (widget.label0 != '') {
results.add(widget.label0);
}
if (widget.label1 != '') {
results.add(widget.label1);
}
if (widget.label2 != '') {
results.add(widget.label2);
}
if (widget.label3 != '') {
results.add(widget.label3);
}
if (widget.label4 != '') {
results.add(widget.label4);
}
if (widget.label5 != '') {
results.add(widget.label5);
}
if (widget.label6 != '') {
results.add(widget.label6);
}
if (widget.label7 != '') {
results.add(widget.label7);
}
if (widget.label8 != '') {
results.add(widget.label8);
}
if (widget.label9 != '') {
results.add(widget.label9);
}
if (widget.label10 != '') {
results.add(widget.label10);
}
if (widget.label11 != '') {
results.add(widget.label11);
}
if (widget.label12 != '') {
results.add(widget.label12);
}
if (widget.label13 != '') {
results.add(widget.label13);
}
if (widget.label14 != '') {
results.add(widget.label14);
}
if (widget.label15 != '') {
results.add(widget.label15);
}
if (widget.label16 != '') {
results.add(widget.label16);
}
if (widget.label17 != '') {
results.add(widget.label17);
}
if (widget.label18 != '') {
results.add(widget.label18);
}
if (widget.label19 != '') {
results.add(widget.label19);
}
if (widget.label20 != '') {
results.add(widget.label20);
}
if (widget.label21 != '') {
results.add(widget.label21);
}
if (widget.label22 != '') {
results.add(widget.label22);
}
if (widget.label23 != '') {
results.add(widget.label23);
}
if (widget.label24 != '') {
results.add(widget.label24);
}
if (widget.label25 != '') {
results.add(widget.label25);
}
if (widget.label26 != '') {
results.add(widget.label26);
}
if (widget.label27 != '') {
results.add(widget.label27);
}
if (widget.label28 != '') {
results.add(widget.label28);
}
if (widget.label29 != '') {
results.add(widget.label29);
}
if (widget.label30 != '') {
results.add(widget.label30);
}
if (widget.label31 != '') {
results.add(widget.label31);
}
if (widget.label32 != '') {
results.add(widget.label32);
}
if (widget.label33 != '') {
results.add(widget.label33);
}
if (widget.label34 != '') {
results.add(widget.label34);
}
if (widget.label35 != '') {
results.add(widget.label35);
}
if (widget.label36 != '') {
results.add(widget.label36);
}
if (widget.label37 != '') {
results.add(widget.label37);
}
}
Timer.periodic(Duration(seconds: 5), (Timer timer) {
// if (_mytimerz = true) {
// timer.cancel();
// }
// if (_speedtime = false) {
// Timer.periodic(Duration(seconds: 2), (timer) {});
if (_currentPage < results.length) {
_currentPage++;
// _currentPage++;
}
// _speedtime = true;
// timer.cancel();
// } else {
// GestureDetector(onPanUpdate: (details) {
// if (details.delta.dx > 0 || details.delta.dx < 0) {
// print("right");
// }
// });
// _currentPage = _currentPage;
// // 0
// }
_pageController
.
// animateTo(
// _pageController.,
// duration: , curve: curve)
animateToPage(
// results.length,
_currentPage = _currentPage,
duration:
// _speedtime
// ?
// Duration(milliseconds: 0),
// :
Duration(milliseconds: 350),
curve: Curves.easeIn,
);
}
// );
// );
// }
);
}
First, have a variable to feed the page view index like pageViewIndex
Initialise the PageController in the initState method with the pageViewIndex
Then in the PageView's onPageChanged method do setState for the pageViewIndex and animate the PageController to the (new) pageViewIndex
Hope that should do.
Comment down if any other help is needed.
i try to reconnect with the server , the connection established very well and it work every 5 sec very well also , but when i put a condiotn to control the conection it not working and still get new conection added , new connection added
how can i implement this by dart
var nbmer = 0;
var channel;
Timer? timer;
connect() {
Socket.connect(IP, PORT).then((Socket sock) async {
channel = sock;
sock.write('hello world'
);
});
}
try {
if (nbmer == 0) {
timer = Timer.periodic(Duration(seconds: 5), (Timer t) => connect());
}
} catch (e) {
print(e.toString());
}
nbmer += 1;
You should put your condition in the connect() function, because Timer.periodic will be periodically executed and not check your condition (if (nbmer == 0)).
Solution
Your code should look something like this:
var nbmer = 0;
var channel;
Timer? timer;
connect() {
if (nbmer == 0) {
nbmer += 1;
Socket.connect(IP, PORT).then((Socket sock) async {
channel = sock;
sock.write('hello world');
});
}
}
try {
timer = Timer.periodic(Duration(seconds: 5), (Timer t) => connect());
} catch(e) {
print(e.toString());
}
I am trying to make snake 2 in flutter. And I have used Timer.periodic() for game loop. And I tried specifying duration as 1 seconds. But the code inside the Timer.periodic() runs multiple times in a second. I also tried debugging (though I am terrible at that) and found that the code inside the Timer.periodic() ran multiple times without stepping out of it. Though while debugging this couild happen as the code pauses for input. But I'm not sure about anything .Here is my code -
import 'dart:async';
import 'dart:math';
import 'package:flutter/material.dart';
class SnakePage extends StatefulWidget {
#override
_SnakePageState createState() => _SnakePageState();
}
class _SnakePageState extends State<SnakePage> {
int score = 0;
String swipe = '';
bool running = false;
int iterates = 0;
List snake = [
[
[4, 3],
1,
true
],
[
[4, 2],
1,
false
],
[
[4, 1],
1,
false
],
];
// Convert radians to degree
double radians(double degree) {
return ((degree * 180) / pi);
}
void turn(moveEvent) {
double angle = radians(moveEvent.delta.direction);
if (angle >= -45 && angle <= 45) {
this.swipe = 'Swipe Right';
} else if (angle >= 45 && angle <= 135) {
this.swipe = 'Swipe Down';
} else if (angle <= -45 && angle >= -135) {
this.swipe = 'Swipe Up';
} else {
this.swipe = 'Swipe Left';
}
}
int toIndex(coOrdinates) {
return ((coOrdinates[0] + 1) * 10) + coOrdinates[1];
}
void run() {
this.running = true;
Timer.periodic(
Duration(
milliseconds: 500,
), (timer) {
this.setState(() {
this.iterates += 1;
this.swipe = this.iterates.toString();
for (var i = 0; i < this.snake.length; i++) {
this.snake[i][0][1] += 1;
if (this.snake[i][0][1] == 10) {
this.snake[i][0][1] = 0;
}
}
});
});
}
#override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('WC'),
),
body: Listener(
onPointerMove: this.running
? (moveEvent) => this.turn(moveEvent)
: (moveEvent) => this.run(),// Where the function is being called
child: Container();
);
}
}
And please pardon me for code being a mess and not well commented.
Any Help would be appreciated!
The problem is that, every time you execute the run() method, a new timer is created and you listen for it, again. The old timer is not stopped, so it keeps firing.
The solution is, before you create a timer, cancel the previous one. Something like this:
class _SnakePageState extends State<SnakePage> {
Timer? _myTimer;
void run() {
this.running = true;
_myTimer?.cancel(); //in case we have a timer, we'll cancel it.
_myTimer = Timer.periodic(. // assing new timer to our variable.
Duration(
milliseconds: 500,
), (timer) {
this.setState(() {
this.iterates += 1;
this.swipe = this.iterates.toString();
for (var i = 0; i < this.snake.length; i++) {
this.snake[i][0][1] += 1;
if (this.snake[i][0][1] == 10) {
this.snake[i][0][1] = 0;
}
}
});
});
}
}
when I call timer.cancel (), the clock stops working. But when calling back to start a new session, I discover that the _u variable in the old session still counts from itself and parallel with new one.
Please help !
startTime(60);
timer.cancel();# It worked.
timer = null;
startTime(60);# but when recall. It working with 2 session. (old and new)
//Call timer.cancel() _u variable stop at 40.
//Recall startTime() _u variable counts with 2 session (40) and (60)
startTime(reset){
int _u = reset;
const oneSec = const Duration(seconds: 1);
timer = Timer.periodic(
oneSec,
(Timer timer) => setState(
() {
if (_u < 1) {
timer.cancel();
// call my handle
} else {
_u = _u -1;
if(_u <0){
timer?.cancel();
}
}
},
),
);
Cancel the timer if it is not null. The code below was tested
void startTime(reset) {
int _u = reset;
const oneSec = const Duration(seconds: 1);
if (timer != null) {
timer.cancel();
}
timer = Timer.periodic(oneSec, (Timer t) {
if (_u < 1) {
t.cancel();
// call my handle
} else {
_u = _u - 1;
print(_u);
if (_u < 0) {
t?.cancel();
}
}
});
}
I am trying to achieve a direct task, I want to autoplay the next track after the current track finishes, but sometimes the onComplete event is triggered before even the track is completed which lead to skip one of the tracks
the package I am using is: audioplayers ^0.14.2
the tracks are fetched from the database they are not local tracks
void play(List<SoundTrack> tracks){
audioPlayer.play(tracks[currentIndex].url);
setIsTalking = true;
audioPlayer.onPlayerCompletion.listen((event) {
if(currentIndex < tracks.length-1) {
next(tracks);
} else {
audioPlayer.release();
setIsTalking = false;
setPlayerState = PlayerState.paused;
}
print('completed this track, current index is' + currentIndex.toString());
});
}
void next(List<SoundTrack> tracks){
setCurrentIndex = currentIndex +1;
play(tracks);
}```
Try something like this:
define
int currentIndex = 0;
and
play(List<String> urlList, int currentIndex) async {
int result = await audioPlayer.play(urlList[currentIndex]);
if (result == 1) {
print('Success: is playing');
} else {
print('Error on audio play');
}
audioPlayer.onPlayerCompletion.listen((event) {
if(currentIndex < urlList.length-1){
currentIndex = currentIndex + 1;
nextTrack(urlList, currentIndex);
print("NEXT AUDIO! $currentIndex");
} else {
print("AUDIO COMPLETED PLAYING");
}
});
}
void nextTrack(List<String> urlList, int currentIndex) {
play(urlList, currentIndex);
}
In your play botton you call
nextTrack(urlList, currentIndex)
I tested and for me it is working.