Home > database >  Exception has occurred. StateError (Bad state: field does not exist within the DocumentSnapshotPlatf
Exception has occurred. StateError (Bad state: field does not exist within the DocumentSnapshotPlatf

Time:12-29

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?

CodePudding user response:

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"];
      });
  }

CodePudding user response:

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}');
      },
      one rror: (e) => print("Error getting document: $e"),
    );
  }

  • Related