מוסיפים את האפליקציה לפרויקט Firebase במסוף Firebase.
יצירת מסד נתונים
עוברים לקטע מסד נתונים בזמן אמת במסוף Firebase. תתבקשו לבחור פרויקט קיים ב-Firebase. פועלים לפי תהליך העבודה ליצירת מסד נתונים.
יש לבחור מצב התחלה עבור כללי האבטחה:
מצב בדיקה
מתאימים לתחילת העבודה עם ספריות לקוח לנייד ולאינטרנט, אבל הוא מאפשר לכל אחד לקרוא ולהחליף את הנתונים שלכם. לאחר הבדיקה, מבצעים חשוב לקרוא את הכללים בנושא מסדי נתונים בזמן אמת ב-Firebase.
כדי להתחיל, בוחרים באפשרות 'מצב בדיקה'.
מצב נעילה
דוחה את כל פעולות הקריאה והכתיבה ללקוחות בנייד ובאינטרנט. שרתי האפליקציות המאומתים עדיין יכולים לגשת למסד הנתונים.
צריך לבחור אזור למסד הנתונים. בהתאם לאזור שבחרתם, מרחב השמות של מסד הנתונים יהיה <databaseName>.firebaseio.com או <databaseName>.<region>.firebasedatabase.app. מידע נוסף זמין במאמר בחירת מיקומים לפרויקט.
לוחצים על סיום.
כשמפעילים את 'מסד נתונים בזמן אמת', הוא גם מפעיל את ה-API Cloud API Manager
הוספת מסד נתונים בזמן אמת ב-Firebase לאפליקציה
מהרמה הבסיסית (root) של פרויקט Flutter, מריצים את הפקודה הבאה כדי להתקין את הפלאגין:
flutterpubaddfirebase_database
בסיום התהליך, בונים מחדש את אפליקציית Flutter:
flutterrun
להגדיר כללים למסדי נתונים
מסד הנתונים 'זמן אמת' מספק שפה של כללים הצהרתיים שמאפשרת לכם: מגדירים את המבנה של הנתונים, איך צריך להוסיף אותם לאינדקס ומתי שאפשר לקרוא מהם את הנתונים ולכתוב בהם.
איך מפעילים את החבילה של Firebase Realtime Database
כדי להתחיל להשתמש בחבילה של מסד נתונים בזמן אמת בפרויקט, צריך לייבא אותה בכתובת החלק העליון של קובצי הפרויקט:
[[["התוכן קל להבנה","easyToUnderstand","thumb-up"],["התוכן עזר לי לפתור בעיה","solvedMyProblem","thumb-up"],["סיבה אחרת","otherUp","thumb-up"]],[["חסרים לי מידע או פרטים","missingTheInformationINeed","thumb-down"],["התוכן מורכב מדי או עם יותר מדי שלבים","tooComplicatedTooManySteps","thumb-down"],["התוכן לא עדכני","outOfDate","thumb-down"],["בעיה בתרגום","translationIssue","thumb-down"],["בעיה בדוגמאות/בקוד","samplesCodeIssue","thumb-down"],["סיבה אחרת","otherDown","thumb-down"]],["עדכון אחרון: 2025-07-25 (שעון UTC)."],[],[],null,["\u003cbr /\u003e\n\nPrerequisites\n\n1. [Install `firebase_core`](/docs/flutter/setup) and add the initialization code to your app if you haven't already.\n2. Add your app to your Firebase project in the [Firebase console](https://console.firebase.google.com/).\n\nCreate a Database\n\n1. Navigate to the **Realtime Database** section of the [Firebase console](https://console.firebase.google.com/project/_/database).\n You'll be prompted to select an existing Firebase project.\n Follow the database creation workflow.\n\n2. Select a starting mode for your security rules:\n\n **Test mode**\n\n Good for getting started with the mobile and web client libraries,\n but allows anyone to read and overwrite your data. After testing, **make\n sure to review the [Understand Firebase Realtime Database Rules](/docs/database/security)\n section.**\n | **Note:** If you create a database in Test mode and make no changes to the default world-readable and world-writeable security rules within a trial period, you will be alerted by email, then your database rules will deny all requests. Note the expiration date during the Firebase console setup flow.\n\n To get started, select testmode.\n\n **Locked mode**\n\n Denies all reads and writes from mobile and web clients.\n Your authenticated application servers can still access your database.\n3. Choose a region for the database. Depending on your choice of region,\n the database namespace will be of the form `\u003cdatabaseName\u003e.firebaseio.com` or\n `\u003cdatabaseName\u003e.\u003cregion\u003e.firebasedatabase.app`. For more information, see\n [select locations for your project](/docs/projects/locations#rtdb-locations).\n\n4. Click **Done**.\n\nWhen you enable Realtime Database, it also enables the API in the\n[Cloud API Manager](https://console.cloud.google.com/projectselector/apis/api/firebasedatabase.googleapis.com/overview).\n\nAdd Firebase Realtime Database to your app\n\n1. From the root of your Flutter project, run the following command to install the plugin:\n\n flutter pub add firebase_database\n\n2. Once complete, rebuild your Flutter application:\n\n flutter run\n\nConfigure database rules\n\nThe Realtime Database provides a declarative rules language that allows you to\ndefine how your data should be structured, how it should be indexed, and when\nyour data can be read from and written to.\n| **Note:** By default, read and write access to your database is restricted so only authenticated users can read or write data. To get started without setting up Firebase Authentication, you can [configure your rules for public access](/docs/rules/basics#default_rules_locked_mode). This does make your database open to anyone, even people not using your app, so be sure to restrict your database again when you set up authentication.\n\nInitialize the Firebase Realtime Database package\n\nTo start using the Realtime Database package within your project, import it at\nthe top of your project files: \n\n import 'package:firebase_database/firebase_database.dart';\n\nTo use the default Database instance, call the `instance`\ngetter on `FirebaseDatabase`: \n\n FirebaseDatabase database = FirebaseDatabase.instance;\n\nIf you'd like to use it with a secondary Firebase App, use the static `instanceFor` method: \n\n FirebaseApp secondaryApp = Firebase.app('SecondaryApp');\n FirebaseDatabase database = FirebaseDatabase.instanceFor(app: secondaryApp);\n\nIf you'd like to use a different RTDB instance on the same project, you can pass in a `databaseUrl` using\nthe static `instanceFor` method: \n\n final firebaseApp = Firebase.app();\n final rtdb = FirebaseDatabase.instanceFor(app: firebaseApp, databaseURL: 'https://your-realtime-database-url.firebaseio.com/');\n\nNext Steps\n\n- Learn how to [structure data](/docs/database/flutter/structure-data) for Realtime Database.\n\n- [Scale your data across multiple database instances.](/docs/database/usage/sharding)\n\n- [Read and write data.](/docs/database/flutter/read-and-write)\n\n- [View your database in the\n Firebase console.](//console.firebase.google.com/project/_/database/data)"]]