'How do i get the single field value from querysnapshot in flutter firestore

actually I need to get access to the field called userid which in my firestore collection document but I cant access it. so, please can anyone help me with this problem?the print statement only prints the data in json..

void logIn(String email, String password) async {
    if (_formKey.currentState!.validate()) {
      FirebaseFirestore.instance
          .collection("users")
          .where("username", isEqualTo: email)
          .where("password", isEqualTo: password)
          .get()
          .then((QuerySnapshot querySnapshot) => {
                if (querySnapshot.docs.isNotEmpty)
                  {
                    print(querySnapshot.docs.first.data()),

                    Navigator.pushReplacement(
                        context,
                        MaterialPageRoute(
                          builder: (_) =>
                              HomeScreen(userId: querySnapshot.docs.first.id),

this is the out put I get. but i need only the userid field:



Solution 1:[1]

Calling data() on a DocumentSnapshot, gives you all the fields from that document. It unfortunately does so as a generic type T?, which is probably where you are struggling.

To get a specific field, you'll want to cast the data() to a Map and then look up that field:

var data = querySnapshot.docs.first.data() as Map;
var value = data["userId"];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Frank van Puffelen