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

能否通过Flutter与Python读写同一Firebase实时数据库?

Can Flutter and Python Share & Interact with the Same Firebase Realtime Database?

Absolutely! This is fully supported and incredibly straightforward—Firebase Realtime Database is built from the ground up to work across multiple platforms and languages, so pairing Flutter (for mobile/UI apps) and Python (for backend scripts, automation, data processing, etc.) with the same database instance is totally feasible.

Here’s How to Make It Work

1. Prep Your Firebase Project

First, make sure you’ve got a Firebase project with a Realtime Database set up. You’ll need separate credentials for Flutter and Python:

  • For Flutter: Grab the google-services.json (Android) or GoogleService-Info.plist (iOS) from your Firebase console’s project settings.
  • For Python: Generate a service account key (JSON file) via "Project Settings > Service Accounts" in the Firebase console.

2. Flutter Side: Read/Write Operations

Use the official firebase_database package (add it to your pubspec.yaml first). Here’s a quick snippet to get you started:

Writing Data

import 'package:firebase_database/firebase_database.dart';

final dbRef = FirebaseDatabase.instance.ref();

// Add a new user to the database
await dbRef.child('users/123').set({
  'name': 'John Doe',
  'email': 'john.doe@example.com'
});

Reading Data (Real-Time)

// Listen for live updates on the user entry
dbRef.child('users/123').onValue.listen((event) {
  final userData = event.snapshot.value as Map<dynamic, dynamic>;
  print("User's Name: ${userData['name']}");
});

3. Python Side: Read/Write Operations

Use the firebase-admin SDK. Install it first with:

pip install firebase-admin

Then initialize the SDK and interact with the database:

Initialize the SDK

import firebase_admin
from firebase_admin import credentials
from firebase_admin import db

# Load your service account key and initialize the app
cred = credentials.Certificate('path/to/your-service-account-key.json')
firebase_admin.initialize_app(cred, {
    'databaseURL': 'https://your-project-id.firebaseio.com/'
})

Writing Data

user_ref = db.reference('users/456')
user_ref.set({
    'name': 'Jane Smith',
    'email': 'jane.smith@example.com'
})

Reading Data

# Fetch a single user's data
user_data = db.reference('users/456').get()
print(f"User's Email: {user_data['email']}")

Critical Things to Keep in Mind

  • Database Security Rules: Don’t forget to set up proper rules! For development, you can use temporary open rules (but never in production):
    {
      "rules": {
        ".read": true,
        ".write": true
      }
    }
    
    For production, lock down access using authentication (Firebase Auth) to ensure only authorized users can read/write data.
  • Real-Time Sync: Both platforms will get instant updates if you use listeners—any change made by Flutter will show up in Python (and vice versa) right away.
  • Concurrent Writes: Firebase handles simultaneous updates automatically with its conflict resolution system, so you don’t have to stress about data corruption from parallel writes.

Final Thoughts

This setup is super common and reliable. Whether you’re building a Flutter mobile app and using Python for batch data imports, backend automation, or analytics, the integration works seamlessly. Go ahead and implement it—you won’t run into any roadblocks here!

内容的提问来源于stack exchange,提问作者Sohil Sarode

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.09 21:42:59