You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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:

1. Double-Check You're Using the Correct Firebase Database Instance

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.instance instead of FirebaseFirestore.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();
2. Ensure Your Rules Are Actually Published

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).
3. Refresh Your Firebase Configuration Files

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) and ios/Runner (for plist) directories.
  • For Android, make sure your app/build.gradle uses 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'
4. Test Rules with the Firebase Simulator

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.
5. Clear App Cache & Restart

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 with flutter run
6. Check for Code Typos

Double-check the code where you're calling set():

  • Make sure you're using a DatabaseReference (from firebase_database), not a Firestore DocumentReference
  • 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 15:17:54