Lucas Knötig
Lucas Knötig

Reputation: 122

The getter 'documents' isn't defined for the type 'QuerySnapshot' error in Flutter Firebase

So i want to implement role based authentication to my app in flutter. Only users with the permissions should get to this site. Im getting the error:

The getter 'documents' isn't defined for the type 'QuerySnapshot<Map<String, dynamic>>'.
Try importing the library that defines 'documents', correcting the name to the name of an existing getter, or defining a getter or field named 'documents'.

Can anybody help me here?

Code:

class AutoLoginHandler extends StatelessWidget {
  @override
  authorizeAccess(BuildContext context) {
    FirebaseAuth.instance.currentUser!().then((user) {
      FirebaseFirestore.instance
          .collection('/users')
          .where('uid', isEqualTo: user.uid)
          .get()
          .then((docs) {
        if (docs.documents[0].data.exists) {
          if (docs.documents[0].data['role'] == 'admin') {
            Navigator.of(context).push(new MaterialPageRoute(
                builder: (BuildContext context) => new LehrerMainPage()));
          }
        }
      });
    });
  }
}

Upvotes: 0

Views: 309

Answers (1)

Mehmet Demir
Mehmet Demir

Reputation: 283

try this. But I suggest to you to use for role management access always use Custom Claims.

.then((results) {
   if (results.size > 0) {
          if (results.docs[0].data()['role'] == 'admin') 
         {
            Navigator.of(context).push(new 
   MaterialPageRoute(
                builder: (BuildContext context) => new 
   LehrerMainPage()));
          }
   }
}

Upvotes: 1

Related Questions