I am using the connectivity
plugin in my flutter to check for the connection status, but occasionally hitting the error PlatForm Exception(No active stream to cancel, null)
even though i have handled the null case. I have subscribed to the stream in initState
and cancelled the subscription in dispose
state
my code looks something like this.
StreamSubscription streamConnectionStatus;
------------
//remaining code
------------
@override
void initState() {
getConnectionStatus();
}
getConnectionStatus() async {
streamConnectionStatus = new Connectivity()
.onConnectivityChanged
.listen((ConnectivityResult result) {
// Got a new connectivity status!
if (result == ConnectivityResult.mobile ||
result == ConnectivityResult.wifi) {
setState(() {
boolHasConnection = true;
});
} else {
setState(() {
boolHasConnection = false;
});
}
});
@override
void dispose() {
try {
streamConnectionStatus?.cancel();
} catch (exception, stackTrace) {
print(exception.toString());
updateError(exception.toString(), stackTrace);
} finally {
super.dispose();
}
}
this is actually driving me crazy but i am guessing i am missing something or do i have to change the code.
Many thanks, Mahi
As stated in the error, "to fix this problem, create a new project by running flutter create -t app and then move the dart code, assets and pubspec. yaml to the new project." What you need to do is run the command line command `flutter create -t app ' (where is the location of the new project.
We already have a SnackBar widget on Flutter to show such errors or warning messages. To display it using ScaffoldMessenger . Inside the SnackBar, the content is a simple text. If you click on the show message button.
You subscribe to the stream by calling the listen function and supplying it with a Function to call back to when there's a new value available, commonly referred to as a callback function, or just a callback.
periodic will work in your flutter applications. It shows when the app we are going to build has a background color that changes over time. It also displays an increasing number in the center of the screen. We can stop these relentless behaviors by pressing the floating button.
I encountered a similar issue. This is what helped me.
I had subscribed the stream exposed by connectivity plugin in different widgets in the same widget tree. I removed the subscription from child widgets and retained the subscription only in the parent and passed on the connection status to the children from parent.
By doing so my code got more cleaner and the stream subscription was maintained / disposed only at one place. Then I didn't encounter this issue any more.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With