Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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) {

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

  • ‘Widget’ is from ‘package:flutter/src/widgets/framework.dart’ (‘/C:/src/flutter/packages/flutter/lib/src/widgets/framework.dart’).
    builder: (BuildContext context, AsyncSnapshot snapshot) {
    ^

FAILURE: Build failed with an exception.

  • Where:
    Script ‘C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle’ line: 1156

  • What went wrong:
    Execution failed for task ‘:app:compileFlutterBuildDebug’.

Process ‘command ‘C:\src\flutter\bin\flutter.bat” finished with non-zero exit value 1

  • Try:

Run with –stacktrace option to get the stack trace.
Run with –info or –debug option to get more log output.
Run with –scan to get full insights.

BUILD FAILED in 17s
Exception: Gradle task assembleDebug failed with exit code 1

>Solution :

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. But the method requires a widget to be returned so adding a return any widget at the end should resolve it

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
}); //
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading