简体   繁体   English

错误:主体可能正常完成,导致返回“null”,但返回类型“Widget”可能是不可为 null 的类型

[英]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' 不允许为空。

  • 'Widget' is from 'package:flutter/src/widgets/framework.dart' ('/C:/src/flutter/packages/flutter/lib/src/widgets/framework.dart'). “小部件”来自“package:flutter/src/widgets/framework.dart”(“/C:/src/flutter/packages/flutter/lib/src/widgets/framework.dart”)。 builder: (BuildContext context, AsyncSnapshot snapshot) { ^ builder:(BuildContext 上下文,AsyncSnapshot 快照){ ^

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 结束

  • Try:尝试:

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.

相关问题 正文可能会正常完成,导致返回“null”,但返回类型可能是不可为 null 的类型。json flutter - The body might complete normally, causing 'null' to be returned, but the return type is a potentially non-nullable type.. json flutter Flutter 错误:body 可能正常完成,导致返回 &#39;null&#39; - Flutter error: The body might complete normally, causing 'null' to be returned Flutter 错误:必须返回非空值,因为返回类型 'Widget' 不允许 null - Flutter Error: A non-null value must be returned since the return type 'Widget' doesn't allow null 为什么可空的 ArrayList 在将不可空的 ArrayList 传递给它时会出现类型不匹配错误? - Why does a nullable ArrayList give a Type Mismatch error when passing it a non-nullable ArrayList? Flutter - 如何处理小部件属性的非空实例 - Flutter - How to handle Non-Nullable Instance of widget properties 可空值和不可空值 - Nullable and non-nullable values 错误:错误:在类型为View的可为空的接收器上仅允许安全(?。)或非空声明(!!。)调用? - Error:error: only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type View? 无法将不可为空的 LiveData 值设置为 null - Cannot set non-nullable LiveData value to null 为什么可以将 java 可空返回值分配给 Kotlin 非可空变量? - Why can a java nullable returned value be assigned to a Kotlin Non-Nullable variable? 不可为空的实例字段 - Non-Nullable instance field
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM