Flutter集成Firebase数据库遇Permission denied错误,求解决方案
Hey there, let's dig into why you're hitting that Permission denied error even with open database rules. I've run into this exact issue before, so here's how to troubleshoot and fix it step by step:
It's easy to mix up Firebase Realtime Database and Cloud Firestore (they sound similar but are entirely separate services).
- Confirm your Flutter code is interacting with Realtime Database, not Firestore. For example, make sure you're using
FirebaseDatabase.instanceinstead ofFirebaseFirestore.instance. - Verify the database URL in your code matches the one in Firebase Console. If you're using a non-default instance or a region-specific database, you need to explicitly specify the URL:
// Correct for custom/region-specific instances FirebaseDatabase.instanceFor(url: "https://your-project-id.firebaseio.com/").reference();
More often than not, the issue is that you edited the rules but forgot to publish them.
- Head to Firebase Console → Realtime Database → Rules tab
- Confirm your rules are exactly:
{ "rules": { ".read": true, ".write": true } }
- Click the Publish button in the top-right corner, then wait 10-15 seconds for the changes to propagate (there's sometimes a small delay).
Outdated google-services.json (Android) or GoogleService-Info.plist (iOS) can cause permission mismatches:
- Download the latest configuration files from Firebase Console → Project Settings → Add app (or re-download for existing apps)
- Replace the old files in your Flutter project's
android/app(for JSON) andios/Runner(for plist) directories. - For Android, make sure your
app/build.gradleuses the Firebase BOM to keep dependencies compatible:
implementation platform('com.google.firebase:firebase-bom:32.7.0') // Use latest version implementation 'com.google.firebase:firebase-database'
Use the built-in simulator to confirm your rules are working as expected:
- In Firebase Console's Rules tab, open the Simulator panel
- Select a Write operation, enter a test path (e.g.,
/test), and click Run - If the simulator returns "Denied", your rules haven't been applied properly. Re-publish and try again.
Device or emulator cache can sometimes hold onto old rule configurations:
- Uninstall the app from your device/emulator, then reinstall it
- Stop your Flutter run session, run
flutter clean, then restart withflutter run
Double-check the code where you're calling set():
- Make sure you're using a
DatabaseReference(fromfirebase_database), not a FirestoreDocumentReference - Verify your path is correct (e.g., no typos in child names)
// Example of correct write operation FirebaseDatabase.instance.reference() .child("users") .child("1") .set({"name": "Qasim"});
The most common fixes here are publishing your rules or correcting the database instance you're using. Start with those two, and you should be able to resolve the Permission denied error.
内容的提问来源于stack exchange,提问作者Qasim Ali

