I am building a Flutter mobile app using Firebase backend. I have four collections: institutions, resources, resourceTypes, users.
I want the institution's collection to be accessible by any user (authenticated/unauthenticated). Because this collection data needs to show the user before signup to take input from the user. Rest collections will be only accessible by authenticated users. Data is fetching and showing unauthenticated users before signup but I am getting the given error:
[ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: PlatformException(firebase_firestore, com.google.firebase.firestore.FirebaseFirestoreException: PERMISSION_DENIED: Missing or insufficient permissions., {code: permission-denied, message: The caller does not have permission to execute the specified operation.}, null)
Below are the function to fetch institution collection data -
late bool isInstitutionsLoading = false;
late List<Institution> institutions = [];
Future<void> fetchInstitutions(BuildContext context) async {
try {
isInstitutionsLoading = true;
notifyListeners();
final snap =
await database.collection('institutions').orderBy('name').get();
institutions =
snap.docs.map((d) => Institution.fromFirestore(d)).toList();
notifyListeners();
} catch (e, st) {
CustomSnack.warningSnack(e.toString(), context);
} finally {
isInstitutionsLoading = false;
notifyListeners();
}
}
Below are my rules:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
match /institutions/{docId} {
allow get, list: if true;
allow create: if request.auth != null;
allow update, delete: if request.auth != null &&
request.auth.uid == resource.data.userId;
}
match /users/{docId} {
allow read: if request.auth != null;
allow create: if request.auth != null;
allow update, delete: if request.auth != null &&
request.auth.uid == resource.data.userId;
}
match /resources/{docId} {
allow read: if request.auth != null;
allow create: if request.auth != null;
allow update, delete: if request.auth != null &&
request.auth.uid == resource.data.userId;
}
match /resourceTypes/{docId} {
allow read: if request.auth != null;
allow create: if request.auth != null;
allow update, delete: if request.auth != null &&
request.auth.uid == resource.data.userId;
}
}
}