Accessing results of multiple futures in futurebuilder

1,650
snapshot.data[1].data[index].data["fname"]

This line should instead be:

snapshot.data[1][index].fname

where fname is a property of the object you are accessing.

Share:
1,650
MihirK98
Author by

MihirK98

Updated on December 21, 2022

Comments

  • MihirK98
    MihirK98 over 1 year

    I am extremely new to flutter and need help.

    I am running to futures in the Future Builder and then trying to access the documents. Unfortunately I am getting an error, code and errors below. How do I access both the different document snapshots? Should my future methods return something? Is there a better way to do this?

    The error is here:

    snapshot.data[1].data[index].data["fname"]
    

    Code:

    class PostsWidget extends StatefulWidget {
    
    @override
      _PostsWidgetState createState() => _PostsWidgetState();
    }
    
    class _PostsWidgetState extends State<PostsWidget> {
      Future getPosts() async {
        var firestore = Firestore.instance;
        QuerySnapshot qn = await firestore.collection("posts").getDocuments();
        return qn.documents;
      }
    
      Future getUsers() async {
        var firestore = Firestore.instance;
        QuerySnapshot qn = await firestore.collection("users").getDocuments();
        return qn.documents;
      }
    
      @override
      Widget build(BuildContext context) {
        return Container(
          child: FutureBuilder(
            future: Future.wait([getPosts(), getUsers()]),
            builder: (_, snapshot) {
              switch (snapshot.connectionState) {
                case ConnectionState.waiting:
                  return Center(child: CircularProgressIndicator());
                default:
                  return ListView.builder(
                    itemCount: snapshot.data[0].length,
                    itemBuilder: (_, index) {
                      return Card(
                          child: Column(
                        children: <Widget>[
                          Container(
                            color: Color(0xff707070),
                            child: Padding(
                              padding: const EdgeInsets.all(4.0),
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  Padding(
                                    padding: const EdgeInsets.all(4.0),
                                    child: Text(
                                      snapshot.data[1].data[index].data["fname"],
                                      style: TextStyle(color: Colors.white),
                                    ),
                                  ),
                                  Padding(
                                    padding: const EdgeInsets.all(4.0),
                                    child: Icon(
                                      Icons.info,
                                      color: Colors.white,
                                    ),
                                  ),
                                ],
                              ),
                            ),
                          )
                        ],
                      ));
                    },
                  );
              }
            },
          ),
        );
      }
    }
    

    Logs/Errors:

    Class 'List<DocumentSnapshot>' has no instance getter 'data'.
    Receiver: Instance(length:1) of '_GrowableList'
    Tried calling: data
    
    • Lalli Garden
      Lalli Garden almost 4 years
      same problem here