PJQuakJag
PJQuakJag

Reputation: 1247

Flutter FutureBuilder Returning Null Error Triggered

I'm trying to return a comment and article field from a given document within a Firestore database. I utilize a future builder to return this data by way of a ListTile.

When I run it, the screen goes red for a split second and the below error is rendered, before the tile is successfully displayed as desired. The error is: A build function returned null.The offending widget is: FutureBuilder Build functions must never return null. To return an empty space that causes the building widget to fill available room, return "new Container()". To return an empty space that takes as little room as possible, return "new Container(width: 0.0, height: 0.0)".

Here is my code:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class PostGetter extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
  body: new Container(
    child: new FutureBuilder(
        future: Firestore.instance
            .collection('post')
            .where('article', isEqualTo: 'lpquVtoRNsu0vBLjNByS')
            .getDocuments(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            if (snapshot.data != null) {
              return new Column(
                children: <Widget>[
                  new Expanded(
                    child: new ListView(
                      children: snapshot.data.documents
                          .map<Widget>((DocumentSnapshot document) {
                        return new ListTile(
                          title: new Text(document['comment']),
                          subtitle: new Text(document['author']),
                        );
                      }).toList(),
                    ),
                  ),
                ],
              );
            } else {
              return new CircularProgressIndicator();
            }
          }
        }),),
);
  }
}

Why woudn't the circular progress indicator satisfy the need to return a widget?

Thanks for the help.

Upvotes: 3

Views: 14403

Answers (2)

Sourav Bapari
Sourav Bapari

Reputation: 1

return FutureBuilder(
          future: FirebaseFirestore.instance.collection('images').doc("walpaper").get(),
          builder: (context, snapshot) {
            if (snapshot.hasData) {

          ////////////////Chack here///////////////

              if (snapshot.data!.data() == null) {
                return SizedBox();
              }

Upvotes: 0

Ashutosh Sharma
Ashutosh Sharma

Reputation: 1579

try this:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';

class PostGetter extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
// TODO: implement build
return new Scaffold(
  body: new Container(
    child: new FutureBuilder(
        future: Firestore.instance
            .collection('post')
            .where('article', isEqualTo: 'lpquVtoRNsu0vBLjNByS')
            .getDocuments(),
        builder: (BuildContext context, AsyncSnapshot snapshot) {
          if (snapshot.hasData) {
            if (snapshot.data != null) {
              return new Column(
                children: <Widget>[
                  new Expanded(
                    child: new ListView(
                      children: snapshot.data.documents
                          .map<Widget>((DocumentSnapshot document) {
                        return new ListTile(
                          title: new Text(document['comment']),
                          subtitle: new Text(document['author']),
                        );
                      }).toList(),
                    ),
                  ),
                ],
              );
            } 
          }else {
              return new CircularProgressIndicator();
            }
        }),),
);
  }
}

else should be on the snapshot.hasdata not on snapshot.data != null

Upvotes: 9

Related Questions