简体   繁体   English

固定错误:如何解决返回类型'StreamController<connectivitystatus> ' 不是匿名闭包错误所定义的 'Stream'</connectivitystatus>

[英]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.我正在按照以下教程了解基于 Internet 连接的连接状态。

link: https://www.filledstacks.com/post/make-your-flutter-app-network-aware-using-provider-and-connectivity-status/链接: 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:在我使用 StreamProvider 的过程结束时,在构建器中我收到以下错误:

error: The return type 'StreamController' isn't a 'Stream', as defined by anonymous closure.错误:返回类型“StreamController”不是匿名闭包定义的“Stream”。

CODE IS AS FOLLOWED: main.dart代码如下: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/完全用作者 git 代码替换我的类型代码,链接如下: 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 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这样做时,在其他教程之一中看到了 sink 和 ZF7B44CFFAFD5C52223D5498196C8A2E7BZ,但由于本教程,我在代码和效率方面更领先。 i just added stream at the end with period and voila.我刚刚在末尾添加了 stream 和句号,瞧。 problem solved.问题解决了。

I hope people will be able to find this solution ready to go for their app:)我希望人们能够为他们的应用程序找到准备好 go 的解决方案:)

FYI: In Migration from v3.x.0 to v4.0.0 of Provider package, The builder and initialBuilder parameters of providers are removed.仅供参考:在从 Provider package 的 v3.x.0 迁移到 v4.0.0 中,移除了提供程序的builderinitialBuilder参数。

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.谢谢你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决错误:Flutter中的“返回类型&#39;SliverStaggeredGrid&#39;不是由匿名闭包定义的&#39;Widget&#39;”? - How to fix error: “ The return type 'SliverStaggeredGrid' isn't a 'Widget', as defined by anonymous closure ” in Flutter? _ $ CustomClass显示错误,如匿名闭包所定义,返回类型&#39;Type&#39;不是&#39;CustomClass&#39;。 ` - _$CustomClass shows error of `The return type 'Type' isn't a 'CustomClass', as defined by anonymous closure. ` 如何解决“返回类型&#39;DocumentReference()&#39;不是&#39;DocumentReference()&#39;,由方法”错误定义? - How to solve “The return type 'DocumentReference ()' isn't a 'DocumentReference ()', as defined by the method” error? 如何解决错误 The operator [] is not defined for the type &#39;object&#39; - How to solve error The operator [] isn't Defined for the type 'object' 如何解决错误:未定义“setState” - How to solve error: 'setState' isn't defined 如何解决错误:“MyInheritedWidget”不是类型 - how to solve Error: 'MyInheritedWidget' isn't a type 如何解决“返回类型'List<text> ? 不是闭包上下文所要求的“小部件”。”</text> - How to solve "The return type 'List<Text>?' isn't a 'Widget', as required by the closure's context." 错误:返回类型“CategoryState”不是“Widget”,这是闭包上下文所要求的 - error: The return type 'CategoryState' isn't a 'Widget', as required by the closure's context 错误:根据闭包上下文的要求,返回类型“NetworkImage”不是“Widget” - error: The return type 'NetworkImage' isn't a 'Widget', as required by the closure's context 错误:返回类型'Iterable<book> ' 不是 ' 列表<book> ',根据闭包上下文的要求。 在 Flutter</book></book> - error: The return type 'Iterable<Book>' isn't a 'List<Book>', as required by the closure's context. in Flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM