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:

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

Leave a Reply