![](/img/trans.png)
[英]The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.. json flutter
[英]error: The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type
我的代碼插入“小部件生成器”時遇到問題。 行鍵builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
我給出這個失敗的代碼是:
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection("Conversas").snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Center(
child: Text("Desculpe. Aconteceu algum erro de nossa parte. =(")
); //Center
}
if(snapshot.connectionState == ConnectionState.waiting){
return Center(
child: Text("Carregando")
); //Center
}
if(snapshot.hasData) {
return CustomScrollView(
slivers: [
CupertinoSliverNavigationBar(
largeTitle: Text('Conversas'),
), //CupertinoSliverNavigationBar
SliverList(
delegate: SliverChildListDelegate(
snapshot.data!.docs.map((DocumentSnapshot document){
return Container();
}).toList())) //SliverChildListDelegate, SliverList
],
); //CustomScrollView
}
}); //StreamBuilder
當我嘗試運行該應用程序時,控制台中出現的日志是:
在調試模式下在 SM G780G 上啟動 lib\main.dart... 運行 Gradle 任務 'assembleDebug'... lib/screens/conversas.dart:13:18: 錯誤:由於返回類型必須返回非空值'Widget' 不允許為空。
FAILURE:構建失敗並出現異常。
其中:腳本 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' 行:1156
出了什么問題:任務“:app:compileFlutterBuildDebug”執行失敗。
Process 'command 'C:\src\flutter\bin\flutter.bat'' 以非零退出值 1 結束
使用 --stacktrace 選項運行以獲取堆棧跟蹤。 使用 --info 或 --debug 選項運行以獲得更多日志輸出。 運行 --scan 以獲得完整的見解。
17 秒內構建失敗異常:Gradle 任務 assembleDebug 失敗,退出代碼為 1
最后返回一個容器或大小的盒子。 如果不滿足任何條件,則它為 null,它不是返回類型小部件。 但是該方法需要返回一個小部件,因此在最后添加一個 return 任何小部件都應該解決它
Widget build(BuildContext context) {
return StreamBuilder<QuerySnapshot>(
stream: FirebaseFirestore.instance.collection("Conversas").snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasError) {
return Center(
child: Text("Desculpe. Aconteceu algum erro de nossa parte. =(")
); //Center
}
if(snapshot.connectionState == ConnectionState.waiting){
return Center(
child: Text("Carregando")
); //Center
}
if(snapshot.hasData) {
return CustomScrollView(
slivers: [
CupertinoSliverNavigationBar(
largeTitle: Text('Conversas'),
), //CupertinoSliverNavigationBar
SliverList(
delegate: SliverChildListDelegate(
snapshot.data!.docs.map((DocumentSnapshot document){
return Container();
}).toList())) //SliverChildListDelegate, SliverList
],
); //CustomScrollView
}
return SizedBox.shrink();// add this. If it doesn't meet any condition then a sized box with no size is returned
}); //
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.