固定错误:如何解决返回类型 'StreamController<ConnectivityStatus>' is not a 'Stream',由匿名关闭错误定义

pri*_*oo7 11 status connectivity mobile-application flutter

我正在按照以下教程了解基于互联网连接的连接状态。

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

现在的问题是,那么我正在尝试实现代码。在我使用 StreamProvider 的过程结束时,在构建器中我收到以下错误:

错误:返回类型“StreamController”不是匿名闭包定义的“Stream”。

代码如下: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(),
        ),
      );
  }
}

Run Code Online (Sandbox Code Playgroud)

用作者的 git 代码完全替换我的类型代码,链接如下:https : //github.com/FilledStacks/flutter-tutorials/tree/master/011-network-sensitive-ui/

我试过谷歌搜索,但对我的情况没有用。我的代码出了什么问题?是因为我在使用另一个供应商吗?


自我发现的解决方案的更新答案


@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(),
        ),
      );
  }
}
Run Code Online (Sandbox Code Playgroud)

我认为它们是从教程发布时对包的更新,因此当我阅读大量文章时,我选择了一个关键字流控制器,对其进行了 RND,然后移至 Stream Provider 并在其上进行了更多 RND这样做时,在其他教程之一中看到了 sink 和 stream,但是由于本教程,我在代码和效率方面遥遥领先。我刚刚在末尾添加了流,瞧!问题解决了。

我希望人们能够找到这个解决方案,准备好用于他们的应用程序:)

小智 19

仅供参考:在从提供程序包的 v3.x.0 到 v4.0.0 的迁移中,提供程序的builderinitialBuilder参数被删除。

前:

StreamProvider( builder: (context) => ConnectivityService().connectionStatusController,
Run Code Online (Sandbox Code Playgroud)

后:

StreamProvider( create: (_) => ConnectivityService().connectionStatusController.stream,
Run Code Online (Sandbox Code Playgroud)