Flutter FutureBuilder Returning Null Error Triggered

10,900

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

Share:
10,900

Related videos on Youtube

PJQuakJag
Author by

PJQuakJag

Updated on September 15, 2022

Comments

  • PJQuakJag
    PJQuakJag over 1 year

    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.

  • PJQuakJag
    PJQuakJag over 5 years
    Amazing! Thank you!