![](/img/trans.png)
[英]How to fix error: “ The return type 'SliverStaggeredGrid' isn't a 'Widget', as defined by anonymous closure ” in Flutter?
[英]Fixed-error: How to Solve The return type 'StreamController<ConnectivityStatus>' isn't a 'Stream', as defined by anonymous closure error
我正在按照以下教程了解基於 Internet 連接的連接狀態。
現在的問題是,然后我正在嘗試實現代碼。 在我使用 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(),
),
);
}
}
完全用作者 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(),
),
);
}
}
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,但由於本教程,我在代碼和效率方面更領先。 我剛剛在末尾添加了 stream 和句號,瞧。 問題解決了。
我希望人們能夠為他們的應用程序找到准備好 go 的解決方案:)
僅供參考:在從 Provider package 的 v3.x.0 遷移到 v4.0.0 中,移除了提供程序的builder
和initialBuilder
參數。
前:
StreamProvider( builder: (context) => ConnectivityService().connectionStatusController,
后:
StreamProvider( create: (_) => ConnectivityService().connectionStatusController.stream,
修復/回答第一篇文章中更新的問題(問題本身)。 謝謝你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.