Home > Net >  How to fix - The body might complete normally causing null to be returned --FLUTTER
How to fix - The body might complete normally causing null to be returned --FLUTTER

Time:02-19

I am using a stream builder to fetch some data from Firebase but it's giving me an error

"The body might complete normally causing 'null' to be returned"

although I have provided a return statement where I am returning the 'data table' widget here is my code

StreamBuilder<QuerySnapshot?>(
              stream: _firestore.collection('cashOut').snapshots(),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  List<DataCell> displayedDataCell = [];

                  for (var item in snapshot.data!.docs) {
                    displayedDataCell.add(
                      DataCell(
                        Text(
                          item['amount'].toString(),
                        ),
                      ),
                    );
                  }
                  return DataTable(
                    columns: const <DataColumn>[
                      DataColumn(
                        label: Text(
                          'Date',
                        ),
                      ),
                      DataColumn(
                        label: Text(
                          'Amount',
                        ),
                      ),
                      DataColumn(
                        label: Text(
                          'Optional Detail',
                        ),
                      ),
                    ],
                    rows: <DataRow>[
                      DataRow(cells: displayedDataCell),
                    ],
                  );
                }
              },
            ),

CodePudding user response:

the builder function requires a widget to return. In your case, you are returning the widget but it is in if condition. This means that if your if condition didn't satisfy, your builder will return null which is not acceptable.

Now, as per other answers, you can handle other states and return things. But if you don't want to handle the other states and you also don't want to show a loader, at the end of your if block, just add return Container(); or return SizedBox();

So, in this way, you won't be showing anything on the screen if the if condition didn't satisfy.

CodePudding user response:

DataTable is only returning when you've data. You also need to handle other states like loading, error...

builder: (context, snapshot) {
  if (snapshot.connectionState == ConnectionState.waiting) {
 /// return your widget while loadling
 } else if (snapshot.hasError) { 
 /// return your widget based on error
  } else if (snapshot.hasData) {
 /// return your widget while have data
  } else if (!snapshot.hasData) {
 /// return your widget while there is no data
  } else {
 /// return  widget
  }
},

CodePudding user response:

The analyzer is quite correct in mentioning that, to explain what is happening is:

  1. The builder function requires a widget to return.
  2. You have handled only one condition.

As per your code, you are only handling the situation where snapshot has data but in case it does not have any data it returns null which is not accepted by the builder function.

A potential solution to your question might be this.

StreamBuilder<QuerySnapshot?>(
              stream: _firestore.collection('cashOut').snapshots(),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  List<DataCell> displayedDataCell = [];

                  for (var item in snapshot.data!.docs) {
                    displayedDataCell.add(
                      DataCell(
                        Text(
                          item['amount'].toString(),
                        ),
                      ),
                    );
                  }
                  return DataTable(
                    columns: const <DataColumn>[
                      DataColumn(
                        label: Text(
                          'Date',
                        ),
                      ),
                      DataColumn(
                        label: Text(
                          'Amount',
                        ),
                      ),
                      DataColumn(
                        label: Text(
                          'Optional Detail',
                        ),
                      ),
                    ],
                    rows: <DataRow>[
                      DataRow(cells: displayedDataCell),
                    ],
                  );
                }
                // While it is loading or has no data
                return CircularProgressIndicator();
              },
           );
  • Related