Fire something inside a listener only if n seconds have passed since receiving last event - flutter

I am listening to an event, however, I don't want to print the event every time. There is an event being sent every second but I don't want my print to work every second. How can I make the print inside this listener to fire only, if 10 seconds is past since last event?
For e.g I receive an event, I use the print. I want to store the event somewhere, if 10 seconds is passed since last event, accept another event -> print and so on.
_controller.onLocationChanged.listen((event) {
print(event);
});

You may try something related to an asynchronous method as such. The following code will set the _isListening variable to true after 10 seconds, which will enable the listener to do it's action once again.
class YourClass{
bool _isListening = true;
void yourMethod() {
_controller.onLocationChanged.listen((event) {
if(_isListening){
_isListening = false;
print(event);
Future.delayed(const Duration(seconds: 10)).then((_) => _isListening=true);
}
});
}
}
Edit: Thanks to #pskink, the proper way to do it would be by using the debounceTime method. So in proper way:
_controller.onLocationChanged
.debounceTime(Duration(seconds: 10))
.listen((event) {
print(event);
});

Use the Timer like below:
Timer(const Duration(seconds: 10), (){
print("...");
});

Related

Cancel execution of method after it starts in Flutter

Consider this method:
Future<void> methodWithAlotOfSteps()async{
callMethodA();
await callMethodB();
...
return ...;
}
which makes some computation. Suppose I want the user to be able to stop this process at any point in time (when he taps cancel button for example).
How can I stop the execution of the above method no matter what line in the method the "program counter" has reached when the user presses cancel.
I am looking for something like methodWithAlotOfSteps.cancel();.
I tried using CancelableCompleter, but even though the Future is cancelled and onCancel method of the completer is called, but the function continues execution.
I know I can set a boolean flag and check it after each "step" ("line", "call to a method"),such as :
Future<void> methodWithAlotOfSteps()async{
if(!completer.isCancelled)
callMethodA();
if(!completer.isCancelled)
await callMethodB();
...
return ...;
}
but is there a better way of doing this?
As #Jamesdlin suggested, the apparent way is to check the cancelable completer's state after each async gap:
class MyService{
CancelableCompleter completer = CancelableCompleter();
Future<void> doSomething() async {
doSomeSyncWork(); // Sync work
doAnotherSyncWork(); // Sync work
await doSomeAsyncWork(); // Async work, this will return control to event loop and make it possible to, for example, press a button to cancel the future.
// here we know we have lost control for a while, so we must check if we have been cancelled
if (completer.isCompleted) {
return;
}
doSomeMoreSyncWork();
await doSomeMoreAsyncWork();
// here we know we have lost control for a while, so we must check if we have been cancelled
if (completer.isCompleted) {
return;
}
...
completer.complete();
}
}

Prevent a Bloc event from being called multiple times within a set period of time

I'm using a QRCode Scanner which triggers the same Event in my Bloc many times a second
In order to prevent spamming my API
-> I'd like cancel / drop all occurence of this event triggered within 5 seconds after the last one was called
Here is my Bloc event :
on<SearchWithQRCode>(_onSearchWithQRCode));
& here is for reference the presentation widget triggering the event
MobileScanner(
allowDuplicates: true,
controller: cameraController,
onDetect: (barcode, args) {
if (barcode.rawValue == null) return;
context.read<ScanQrCodeBloc>().add(
SearchWithQRUrl(qrUrl: barcode.rawValue!),
);
},
),
In this scenario you might be interested in bloc_concurrency & stream_transform
Using this event transformer :
import 'package:stream_transform/stream_transform.dart';
import 'package:bloc_concurrency/bloc_concurrency.dart';
EventTransformer<E> throttleDroppable<E>(Duration duration) {
return (events, mapper) {
return droppable<E>().call(events.throttle(duration), mapper);
};
}
like so :
on<SearchWithQRUrl>(
_onSearchWithQRUrl,
transformer: throttleDroppable(const Duration(seconds: 5)),
);
-> All following calls to this event occurring within the set throttle duration (here 5 seconds) will be canceled

How to set a time-specific event in Flutter?

I am trying to get a switch widget to turn off at a specific time of the day.
I have read a lot of the documentations like these
https://stackoverflow.com/questions/15848214/does-dart-have-a-scheduler
https://pub.dev/packages/cron
https://pub.dev/packages/scheduled_timer
All of these which can only allow me to set duration instead of a specific time.
Thus, my only approach right now is by setting up the timer when the switch is turned on.
e.g. 8hrs then it turns off.
Problem: If the user turned on the switch late, the time that it turns off will also be delayed.
So is there an actual way to set an event at a specific time + works even after we onstop/terminate the application?
You can try to do something like this:
I'll simplify the specific time into :
...
var setTime = DateTime.utc(2022, 7, 11, 8, 48, 0).toLocal();
StreamSubscription? subscription;
...
Then you can assign a periodic stream listener:
...
// periodic to run every second (you can change to minutes/hours/others too)
var stream = Stream.periodic(const Duration(seconds: 1), (count) {
//return true if the time now is after set time
return DateTime.now().isAfter(setTime);
});
//stream subscription
subscription = stream.listen((result) {
// if true, insert function and cancel listen subscription
if(result){
print('turn off');
subscription!.cancel();
}
// else if not yet, run this function
else {
print(result);
}
});
...
However, running a Dart code in a background process is more difficult, here are some references you can try:
https://medium.com/flutter/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124
https://pub.dev/packages/flutter_background_service
I hope it helps, feel free to comment if it doesn't work, I'll try my best to help.
After some time I figured it out.
Format
cron.schedule(Schedule.parse('00 00 * * *'), () async {
print("This code runs at 12am everyday")
});
More Examples
cron.schedule(Schedule.parse('15 * * * *'), () async {
print("This code runs every 15 minutes")
});
To customize a scheduler for your project, read this

Flutter: How to check if stream is empty or has no events?

I'm using flutter_beacon package to scan for the beacons like this,
_streamRanging =
flutterBeacon.ranging(regions).listen((RangingResult result) {
if (result.beacons.isEmpty) {
_streamRanging.cancel();
onBleSearch(false);
}
//do something else
}
);
now as there are no beacons found I want to call onBleSearch(false), but it doesn't go inside the function of .listen((l){}) because I think there are no events to call that function
so how I can check if the stream has no data and call this onBleSearch(false) function?
It's possible to check if the Stream is empty, but be aware that a Stream provides a way to receive a sequence of events. The Stream can be "empty" for an instance, and could possibly hold a value in the next. What you can do here is to continuously listen for events in the Stream and trigger the method depending on your use case.
StreamController.stream.listen(
(event) => print('Value: $event'), // Check if empty
);
Another way is by checking if the List from the Stream has value by using Stream.toList
Use this the .isEmpy to check if a stream is connected
Stream<String>? myStream;
Future<void> checkStream() async {
bool empty = await myStream.isEmpty;
if (empty) {
restart();
setState(() {});
}
}

Flutter stopwatchtimer doesn't respond to changing time

I use this package https://pub.dev/packages/stop_watch_timer in my app to keep track of the music that is playing. However if I want to change the song by changing the time on the stopwatch it says that I have to reset the timer first which I have already done. If I press the button for the second time it works. This is the code:
final StopWatchTimer _stopWatchTimer = StopWatchTimer(
mode: StopWatchMode.countUp,
onChangeRawSecond: (value) => print('onChangeRawSecond $value'),
);
void change_timer_value(int song_index) {
int new_time = TimerState(
song_index: song_index,
record_side: current_side_list(
record_sides[selectedValue], widget.album_data))
.get_start_value();
print(new_time);
_stopWatchTimer.onExecute.add(StopWatchExecute.reset);
_stopWatchTimer.setPresetSecondTime(new_time); // this is where I set new time
}
I don't know how to get around this. I have already created an issue on the creators GitHub but no response. So there's somebody who can help me here
As you mentioned in the github issue, it looks like the root cause of your issue is that the reset action takes place asynchronously, and so hasn't gone through yet by the time you try to set the time.
One way to get around this is to define your own async function which resets the stopwatch, then waits for the action to complete before returning:
Future<void> _resetTimer() {
final completer = Completer<void>();
// Create a listener that will trigger the completer when
// it detects a reset event.
void listener(StopWatchExecute event) {
if (event == StopWatchExecute.reset) {
completer.complete();
}
}
// Add the listener to the timer's execution stream, saving
// the sub for cancellation
final sub = _stopWatchTimer.execute.listen(listener);
// Send the 'reset' action
_stopWatchTimer.onExecute.add(StopWatchExecute.reset);
// Cancel the sub after the future is fulfilled.
return completer.future.whenComplete(sub.cancel);
}
Usage:
void change_timer_value(int song_index) {
int new_time = TimerState(
song_index: song_index,
record_side: current_side_list(
record_sides[selectedValue], widget.album_data))
.get_start_value();
print(new_time);
_resetTimer().then(() {
_stopWatchTimer.setPresetSecondTime(new_time);
});
}
Or (with async/await):
void change_timer_value(int song_index) async {
int new_time = TimerState(
song_index: song_index,
record_side: current_side_list(
record_sides[selectedValue], widget.album_data))
.get_start_value();
print(new_time);
await _resetTimer();
_stopWatchTimer.setPresetSecondTime(new_time);
}