[英]error: The body might complete normally, causing 'null' to be returned, but the return type, 'Widget', is a potentially non-nullable type
I'm having problems with my code inserting a "widget builder".我的代码插入“小部件生成器”时遇到问题。 An error appears in the line key
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
行键
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
My code that is giving this failure is:我给出这个失败的代码是:
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
When I try to run the app, the log that appears in the console is:当我尝试运行该应用程序时,控制台中出现的日志是:
Launching lib\main.dart on SM G780G in debug mode... Running Gradle task 'assembleDebug'... lib/screens/conversas.dart:13:18: Error: A non-null value must be returned since the return type 'Widget' doesn't allow null.在调试模式下在 SM G780G 上启动 lib\main.dart... 运行 Gradle 任务 'assembleDebug'... lib/screens/conversas.dart:13:18: 错误:由于返回类型必须返回非空值'Widget' 不允许为空。
FAILURE: Build failed with an exception. FAILURE:构建失败并出现异常。
Where: Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1156其中:脚本 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' 行:1156
What went wrong: Execution failed for task ':app:compileFlutterBuildDebug'.出了什么问题:任务“:app:compileFlutterBuildDebug”执行失败。
Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1
Process 'command 'C:\src\flutter\bin\flutter.bat'' 以非零退出值 1 结束
Run with --stacktrace option to get the stack trace.
使用 --stacktrace 选项运行以获取堆栈跟踪。 Run with --info or --debug option to get more log output.
使用 --info 或 --debug 选项运行以获得更多日志输出。 Run with --scan to get full insights.
运行 --scan 以获得完整的见解。
BUILD FAILED in 17s Exception: Gradle task assembleDebug failed with exit code 1 17 秒内构建失败异常:Gradle 任务 assembleDebug 失败,退出代码为 1
Return a container or sized box at the end.最后返回一个容器或大小的盒子。 If no condition is met then it's null which is not a return type widget.
如果不满足任何条件,则它为 null,它不是返回类型小部件。 But the method requires a widget to be returned so adding a return any widget at the end should resolve it
但是该方法需要返回一个小部件,因此在最后添加一个 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.