Skip to content

Instantly share code, notes, and snippets.

@sbosell
Last active September 25, 2020 04:07
Show Gist options
  • Save sbosell/b5a1dd561acf322aceaf31d096cb1b7c to your computer and use it in GitHub Desktop.
Save sbosell/b5a1dd561acf322aceaf31d096cb1b7c to your computer and use it in GitHub Desktop.
Long Polling Stream and Need to Cancel Timer
// The focus on/off methods fire. I am not sure how to change the providers so that I can either force dispose or just start/stop the timer?
final MyRequestProvider =
StreamProvider.autoDispose<List<MyRequest>>((ref) {
StreamController<List<MyRequest>> controller =
StreamController<List<MyRequest>>();
ClientResolver clientResolver = ClientResolver();
IServiceClient client = clientResolver.getClient(AppParameters.getBaseUrl());
PendingMy apiRequest = PendingMy();
Timer timer = Timer.periodic(Duration(seconds: 10), (t) {
client.get(apiRequest).then((data) {
controller.add(data.results);
});
});
ref.onDispose(() {
if (timer != null && timer.isActive) {
timer.cancel();
}
controller.close();
});
return controller.stream;
});
// using the https://pub.dev/packages/focus_detector to detect screen on / off
// how do i control the timer and polling in this case?
// somewhere in a build tree:
FocusDetector(
key: _focusDetectorKey,
onFocusGained: () {
},
onFocusLost: () {
debugPrint('CharacterListPage lost focus');
var d = context.read(curbsideRequestProvider);
// d.dispose()?
}_
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment