0
votes

I am trying a flutter app , in which I need to read all docs from a collection called News from Firestore and place it in a list builder

class _CategoriesState extends State<Categories> {
  Future<QuerySnapshot> getcat() {
    return fb.collection("News").get();
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      //appBar: Myappbar(),
      //drawer: navigationDrawer(),
      body: Container(
        padding: EdgeInsets.all(10.0),
        child: FutureBuilder(
          future: getcat(),
          builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              return ListView.builder(
                  scrollDirection: Axis.vertical,
                  physics: BouncingScrollPhysics(),
                  shrinkWrap: true,
                  itemCount: snapshot.data.docs.length,
                  itemBuilder: (BuildContext context, int index) {
                    return Card(
                      child: ExpansionTile(
                        title: Text(snapshot.data.docs[index].data()["Title"]),
                     
                      ),
                    );
                  });
            } else if (snapshot.connectionState == ConnectionState.none) {
              return Text("No data");
            }
            return CircularProgressIndicator();
          },
        ),
      ),
    );
  }
}

This is the code I am using My Firestore enter image description here

I am getting an error

The following NoSuchMethodError was thrown building FutureBuilder(dirty, state: _FutureBuilderState#25db8): The getter 'docs' was called on null. Receiver: null Tried calling: docs

The relevant error-causing widget was: FutureBuilder file:///Users/raviteja/Documents/IND/bubbleburst/lib/main.dart:171:16 When the exception was thrown, this was the stack: #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) #1 _CategoriesState.build. (package:bubbleburst/main.dart:179:44) #2 _FutureBuilderState.build (package:flutter/src/widgets/async.dart:773:55) #3 StatefulElement.build (package:flutter/src/widgets/framework.dart:4612:27) #4 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4495:15)

1

1 Answers

1
votes

Change

  if (snapshot.connectionState == ConnectionState.done)

To:

  if (snapshot.connectionState == ConnectionState.done && snapshot.hasData)

This will prevent the error when the query is done, but doesn't have data, as in your case.

You also have to check the spelling for your query, if you wrote 'news' instead of 'News', or that your getcat() is actually returning data.