joyhossain
joyhossain

Reputation: 3

A build function returned null,The offending widget is: StreamBuilder<QuerySnapshot>

Im new to flutter. Im trying to get items from firestore to be shown in Listview on the app but getting "build function returned null.The offending widget is: StreamBuilder,Build functions must never return null". I just want the list 'post' from firstore shown in listview

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

void main() => runApp(MyApp());


class MyApp extends StatelessWidget {

  @override

   Widget build(BuildContext context) {

    return MaterialApp(
      title: 'Flutter Post App',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primaryColor: Color(0xff543b7a),
      ),
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {

 @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
          icon: Icon(FontAwesomeIcons.hamburger),
        ),
      ),
      body: StreamBuilder(
        stream: Firestore.instance.collection('post').snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            Text('Loading');
          } else {
            return ListView.builder(
              itemCount: snapshot.data.document.length,
              itemBuilder: (context, index) {
                DocumentSnapshot myPost = snapshot.data.documents[index];
                return Stack(
                  children: <Widget>[
                    Container(
                      width: MediaQuery.of(context).size.width,
                      height: 350.0,
                      child: Padding(
                        padding: EdgeInsets.only(top: 8.0, bottom: 8.0),
                        child: Material(
                          color: Colors.white,
                          elevation: 14.0,
                          shadowColor: Color(0x802196f3),
                          child: Column(
                            children: <Widget>[
                              Container(
                                width: MediaQuery.of(context).size.width,
                                height: 200.0,
                                child: Image.network(
                                  '${myPost['image']}',
                                  fit: BoxFit.fill,
                                ),
                              ),
                              SizedBox(
                                height: 10.0,
                              ),
                              Text('${myPost['title']}'),
                              SizedBox(
                                height: 10.0,
                              ),
                              Text('${myPost['subtitle']}'),
                            ],
                          ),
                        ),
                      ),
                    )
                  ],
                );
              },
            );

          },
        },


      ),
    );
  }
}
[enter image description here][1]


  [1]: https://i.sstatic.net/QeSyi.png

A build function returned null.The offending widget is: StreamBuilder.Build functions must never return null.

Upvotes: 0

Views: 1550

Answers (1)

Igor Kharakhordin
Igor Kharakhordin

Reputation: 9903

You missed return:

builder: (context, snapshot) {
          if (!snapshot.hasData) {
            Text('Loading'); // <---- no return here
          } else {
            return ListView.builder(
              itemCount: snapshot.data.documents.length, // <---- documents here
              itemBuilder: (context, index) {
                DocumentSnapshot myPost = snapshot.data.documents[index];

Upvotes: 2

Related Questions