0

i got a problem in this part

snapshot.data!.docs[0].data()['list']

but in visual studio no error notification appears in the problem section, how to fix it? did i miss something?

StreamBuilder(
                            stream:
                                _firestore.collection("utils").snapshots(),
                            builder: (
                              BuildContext context,
                              AsyncSnapshot<
                                      QuerySnapshot<Map<String, dynamic>>>
                                  snapshot,
                            ) {
                              if (snapshot.hasData) {
                                final List<dynamic> _productGroups =
                                    snapshot.data!.docs[0].data()['list']
                                        as List<dynamic>;
                                _productGroups.sort();
                                return GridView.builder(
                                  gridDelegate:
                                      const SliverGridDelegateWithFixedCrossAxisCount(
                                    crossAxisCount: 2,
                                    childAspectRatio: 2,
                                    crossAxisSpacing: 20,
                                    mainAxisSpacing: 20,
                                  ),
                                  itemCount: _productGroups.length,
                                  itemBuilder: (context, index) {
                                    return ProductGroupCard(
                                      name: _productGroups[index] as String,
                                      key: UniqueKey(),
                                    );
                                  },
                                );
                              } else {
                                return const Center(
                                  child: SizedBox(
                                    height: 40,
                                    width: 40,
                                    child: CircularProgressIndicator(
                                      color: ColorPalette.pacificBlue,
                                    ),
                                  ),
                                );
                              }
                            },
                          ),
2
  • i think you have 0 docs. you need to check if snapshot.data.docs isnotempty Commented Jun 8, 2022 at 6:03
  • I think that if you create data directly from the firestore, the data will not be empty Commented Jun 8, 2022 at 8:29

1 Answer 1

1

Try this:

StreamBuilder(
              stream:
              _firestore.collection("utils").snapshots(),
              builder: (BuildContext context,
                  AsyncSnapshot<
                      QuerySnapshot<Map<String, dynamic>>>
                  snapshot,) {
                if (snapshot.connectionState == ConnectionState.waiting) {
                  return Center(
                    child: Container(),
                  );
                }
                return GridView(
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 2,
                    childAspectRatio: 2,
                    crossAxisSpacing: 20,
                    mainAxisSpacing: 20,
                  ),
                children:  snapshot.data!.docs.map((DocumentSnapshot document) {
                  Map<String, dynamic> data =
                  document.data()! as Map<String, dynamic>;
                  return Container(
                   // child: widget data['list']
                  );
                }).toList(),
                );
              },
            ),
Sign up to request clarification or add additional context in comments.

1 Comment

this fixed the error, but the data to be input becomes an error

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.