简体   繁体   中英

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:)

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,

Fix / answer to the problem updated in the first post ( question itself ). Thank you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM