I'm using a library for activity recognition in flutter which creates a stream for activity recognition data that comes from the sensors of the phone, I'm able to start stream but not able to stop it when tab from bottom navigation bar changes.
I'm following this example, so now i want to have a start & stop button which can start & stop the stream listening.
I have created it like this :
Stream<Activity> stream;
StreamController<Activity> streamController = new StreamController();
stream = ActivityRecognition.activityUpdates();
if(!streamController.hasListener){
streamController.addStream(stream);
streamController.stream.listen(onData);
}
Future<void> onData(Activity activity) async {
String formattedDate() {
var date = DateTime.now();
return date.day.toString() +
"/" +
date.month.toString() +
"/" +
date.year.toString() +
" " +
date.hour.toString() +
":" +
date.minute.toString() +
":" +
date.second.toString();
}
insertActivity(RecognizedData(
activity: activity.type.toString(),
confidence: activity.confidence,
dateTime: formattedDate()));
}
And then in dispose & deactivate method i have written code like this:
@override
void dispose() {
streamController.close();
print("STREAM_CLOSED");
super.dispose();
}
@override
void deactivate() {
streamController.close();
print("STREAM_CLOSED");
super.deactivate();
}
But it doesn't stop the streaming when the tab changes from bottom navigation bar, so when i open that tab again it shows "Bad State : Stream has already been listened to", so please help me with how to solve this.
Thanks in advance.