Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stream subscription throwing an error in dart in flutter app?

Tags:

flutter

dart

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

like image 228
Mahi Avatar asked Mar 20 '18 14:03

Mahi


People also ask

How do I fix errors in Flutter?

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.

How do you show error messages in Flutter?

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.

How do I subscribe to a stream Flutter?

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.

How do I stop stream periodic Flutter?

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.


1 Answers

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.

like image 58
Prakash George Avatar answered Oct 29 '22 17:10

Prakash George