skip to Main Content

I need to get data from an array in Firestore.

I’m getting an error:

StateError (Bad state: field does not exist within the DocumentSnapshotPlatform)

when I run the following query on the firestore database:

Future getData() async {
    await FirebaseFirestore.instance
        .collection("users")
        .get()
        .then((QuerySnapshot? querySnapshot) {
      querySnapshot!.docs.forEach((doc) {
        alldata = doc["errors"];
      });
    });
  }

How do I properly make this query?

2

Answers


  1. Chosen as BEST ANSWER

    Solved!

    Future getData() async {
        final docRef =
            await FirebaseFirestore.instance.collection("users").doc(user.uid);
        docRef.get().then(
          (DocumentSnapshot doc) {
            alldata = (doc.data() as Map<String, dynamic>)["errors"];
            print('Получен массив данных: ${alldata}');
            print('Получен массив данных: ${alldata.first}');
          },
          onError: (e) => print("Error getting document: $e"),
        );
      }
    
    

  2. Try this instead :

    Future getData() async {
        final snapshot = await FirebaseFirestore.instance
            .collection("users")
            .get();
    
           querySnapshot!.docs.forEach((doc) {
            alldata = (doc.data() as Map<String, dynamic>)["errors"];
          });
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search