I have a BLoC with an event that repeats every second.
How I can move function in a different thread?
I would use an isolate but I can't find examples that work with this code
class ScanBloc extends Bloc<ScanEvent, ScanState> {
Timer? _timer;
ScanBloc() : super(ScanningState()) {
on<StartScanEvent>((event, emit) {
_startTimer();
});
on<StopScanEvent>((event, emit) {
_stopTimer();
});
}
void _startTimer() {
_timer = Timer.periodic(Duration(seconds: 1), (_) {
//My function
});
}
void _stopTimer() {
_timer?.cancel();
_timer = null;
}
@override
Future<void> close() {
_stopTimer();
return super.close();
}
}