簡體   English   中英

將兩個不同的 Firebase 項目添加到我的 Flutter 項目中

[英]Add two different Firebase projects to my Flutter project

我創建了一個 flutter 項目“示例”,我想添加到 firebase 項目“fire1”和“fire2”。 以正常方式添加 firebase 是通過添加google-services.jsonGoogleServices-Info.plist為 IOS708C684E32CAB993。 那么如何在一個 flutter 項目中添加兩個 firebase?

您可以通過在 flutter 中添加風味來嘗試此操作

https://codewithandrea.com/articles/flutter-flavors-for-firebase-apps/

要將您的 Flutter 應用程序連接到 Firebase 項目,您可以使用:

flutterfire configure

其中,默認情況下會添加lib/firebase_option.dart文件

例如,如果您想集成兩個獨立的 firebase 項目,一個用於dev ,一個用於prod ,您可以創建兩個目錄:

lib/firebase/dev
lib/firebase/prod

然后分別運行這些:

flutterfire configure -p firebase-project-id-dev , -o lib/firebase/dev/firebase_options.dart

flutterfire configure -p firebase-project-id-prod , -o lib/firebase/prod/firebase_options.dart

回答每個命令即將出現的問題,並確保您看到Firebase configuration file lib/firebase/prod/firebase_options.dart generated successfully ,以確保您的 firebase_option 已在正確的目錄中創建。

您可以在 firebase 控制台中找到您的 firebase-project-id: project overview > project setting > General > Project ID

順便說一句,這里有一些關於 flutterfire 配置 [參數] 的有用信息:

Usage: flutterfire configure [arguments]
-h, --help                                  Print this usage information.
-p, --project=<aliasOrProjectId>            The Firebase project to use for this command.
-e, --account=<email>                       The Google account to use for authorization.
-o, --out=<filePath>                        The output file path of the Dart file that will be generated with your Firebase configuration options.
                                            (defaults to "lib/firebase_options.dart")
-i, --ios-bundle-id=<bundleIdentifier>      The bundle identifier of your iOS app, e.g. "com.example.app". If no identifier is provided then an attempt will be made to automatically detect it from your "ios" folder (if it exists).
-m, --macos-bundle-id=<bundleIdentifier>    The bundle identifier of your macOS app, e.g. "com.example.app". If no identifier is provided then an attempt will be made to automatically detect it from your "macos" folder (if it
                                            exists).
-a, --android-app-id=<applicationId>        The application id of your Android app, e.g. "com.example.app". If no identifier is provided then an attempt will be made to automatically detect it from your "android" folder (if it
                                            exists).

Run "flutterfire help" to see global options.

正如您提到的創建應用程序的常規方法,您可以使用 Firebase CLI 以同樣的方式創建 Firebase 應用程序,它會為您工作。 您需要做的就是創建第二個應用程序,但您需要為第二個應用程序指定名稱。 如果在創建 Firebase 實例時不使用名稱,則默認使用首先配置的 Firebase 項目。 或者您可以在 main.dart 中手動指定 FirebaseOptions,如下所示。
下面的代碼將幫助您了解如何在一個應用程序或多個項目中根據需要使用兩個單獨的 Firebase 項目。


initMultipleApp() async {
  if (defaultTargetPlatform == TargetPlatform.android) {
    await Firebase.initializeApp(
        //Android app ID's from firebase console of project 1
        name: 'defaultApp',
        options: const FirebaseOptions(
          apiKey: 'youAPIKey',
          appId: 'youAppId',
          messagingSenderId: 'youMessagingSenderId',
          projectId: 'youProjectId',
          storageBucket: 'youStorageBucket',
        ));
  } else if (defaultTargetPlatform == TargetPlatform.iOS) {
    await Firebase.initializeApp(
        //iOS app ID's from firebase console of project 1
        name: 'defaultApp',
        options: const FirebaseOptions(
          apiKey: 'youAPIKey',
          appId: 'youAppId',
          messagingSenderId: 'youMessagingSenderId',
          projectId: 'youProjectId',
          storageBucket: 'youStorageBucket',
        ));
  }
  //initialize second app here
  if (defaultTargetPlatform == TargetPlatform.android) {
    await Firebase.initializeApp(
        //Android app ID's from firebase console of project 2
        name: 'secondApp',
        options: const FirebaseOptions(
          apiKey: 'youAPIKey',
          appId: 'youAppId',
          messagingSenderId: 'youMessagingSenderId',
          projectId: 'youProjectId',
          storageBucket: 'youStorageBucket',
        ));
  } else if (defaultTargetPlatform == TargetPlatform.iOS) {
    await Firebase.initializeApp(
        //iOS app ID's from firebase console of project 2
        name: 'secondApp',
        options: const FirebaseOptions(
          apiKey: 'youAPIKey',
          appId: 'youAppId',
          messagingSenderId: 'youMessagingSenderId',
          projectId: 'youProjectId',
          storageBucket: 'youStorageBucket',
        ));
  }
}

class Database {
  final FirebaseFirestore _firestore1 =
      FirebaseFirestore.instanceFor(app: Firebase.app('defualtApp'));  //use _firestore1 for data you want to fetch from 1st firebase project
  final FirebaseFirestore _firestore2 =
      FirebaseFirestore.instanceFor(app: Firebase.app('secondApp'));    //use _firestore2 for data you want to fetch from 2st firebase project
  //use _firestore1 and _firestore2 to access the database as needed
}

在上面的代碼中,您將手動分配此基於 CLI 的代碼正在執行的 ID

await Firebase.initializeApp(
options: DefaultFirebaseOptions
    .currentPlatform, //auto choose from firebase_options file present in lib folder
  );

這個DefaultFirebaseOptions所做的是在執行期間基於平台創建 Firebase 應用程序。 如果您使用 CLI 初始化 Firebase,這將在lib/ firebase_options.dart 文件中處理。 如果您手動在 Android 文件夾中添加了 google-services.json 文件,在 iOS 中添加了 GoogleServices-info.plist 文件。 它們還將包括類似的 ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM