Search code examples
flutterstatusmobile-applicationconnectivity

Fixed-error: How to Solve The return type 'StreamController<ConnectivityStatus>' isn't a 'Stream', as defined by anonymous closure error


I was following the below tutorial for connectivity status based on internet connection.

link : https://www.filledstacks.com/post/make-your-flutter-app-network-aware-using-provider-and-connectivity-status/

now the issue is, then i am trying to implement the code. at the end of the process where i am using StreamProvider, in builder i am getting this error of:

error: The return type 'StreamController' isn't a 'Stream', as defined by anonymous closure.

CODE IS AS FOLLOWED: main.dart

@override
  Widget build(BuildContext context) {
      return StreamProvider(
        builder:  (context) => ConnectivityService().connectionStatusController, // ERROR LINE
        child: ChangeNotifierProvider<ThemeChanger>(
          builder: (_) => ThemeChanger((x) ? ThemeChanger.customDarkTheme : ThemeChanger.customLightTheme),
          child: new MaterialAppWithTheme(),
        ),
      );
  }
}

completely replacing my type code with the authors git code, link below: https://github.com/FilledStacks/flutter-tutorials/tree/master/011-network-sensitive-ui/

i tried google search but no use for my case. what is going wrong in my code ? is it because i am using another provider ?


UPDATED ANSWER AS SOLUTION FOUND BY SELF DISCOVERY


@override
  Widget build(BuildContext context) {
      return StreamProvider(
        builder:  (context) => ConnectivityService().connectionStatusController.stream, // add .stream at end
        child: ChangeNotifierProvider<ThemeChanger>(
          builder: (_) => ThemeChanger((x) ? ThemeChanger.customDarkTheme : ThemeChanger.customLightTheme),
          child: new MaterialAppWithTheme(),
        ),
      );
  }
}

I think their was an update to the package from the time the Tutorial was published and so as i was going through lots of article i picked up a keyword stream Controller, did RND on it and then move to Stream Provider and did some more RND on this and when doing it saw sink and stream in one of other tutorial but as i was way more ahead in code and efficient due to this tutorial. i just added stream at the end with period and voila ! problem solved.

I hope people will be able to find this solution ready to go for their app :)


Solution

  • FYI: In Migration from v3.x.0 to v4.0.0 of Provider package, The builder and initialBuilder parameters of providers are removed.

    Before:

    StreamProvider( builder: (context) => ConnectivityService().connectionStatusController,
    

    After:

    StreamProvider( create: (_) => ConnectivityService().connectionStatusController.stream,