Flutter Firebase - FlutterFire basic configuration

Flutter integrates Firebase

FlutterFire is a suite of Flutter plugins that connect Flutter apps to Firebase.

First create a Firebase project

This step can follow the process of Firebase

Android installation

Before using FlutterFire on Android, you must first connect to your Firebase project using an Android app.

Generate Firebase project configuration files

On the Firebase console, add a new Android application or select an existing Android application to your Firebase project.

After registering your Android app, download the configuration file from the Firebase console (the file is called google-services.json). Add this file to the android/app directory in your Flutter project.

Install the Firebase configuration file

In order to allow Firebase to use configurations on Android, a plugin must be applied on the project google-services. This requires modifying two files in the android/ directory.

First, google-servicesadd the plugin as a dependency inside the android/build.gradle file:

buildscript {
    
    
  dependencies {
    
    
    // ... other dependencies
    classpath 'com.google.gms:google-services:4.3.3'
  }
}

Finally, add the following to the /android/app/build.gradle file to execute the plugin:

apply plugin: 'com.android.application'
//add this
apply plugin: 'com.google.gms.google-services'

build for android

Due to the high number of classes in some Firebase SDKs (notably Firestore), it can push you beyond the limit of 64k methods on the Android build system, and you might get an error saying Error: Number of method references when merging dex files.dex files The value in can't exceed 64K.

If you do get this error, we recommend enabling Multidex for Android.

Enable Multidex

If your app only targets Android 21 or higher (minSdkVersion), multidex is already enabled by default, and you don't need the multidex support library.

However, if your minSdkVersion is set to 20 or lower, you must use the multidex support library and make the following modifications to your application project:

Open the /android/app/build.gradle file. Add multidex module under dependencies and enable it in defaultConfig:

android {
    
    
    defaultConfig {
    
    
        // ...
        minSdkVersion 16
        targetSdkVersion 28
        //add this
        multiDexEnabled true
    }
}

dependencies {
    
    
	//add this
  implementation 'com.android.support:multidex:1.0.3'
}

Please visit the official Android documentation to learn more.

iOS installation

Before using FlutterFire on iOS, you must first connect to your Firebase project using an iOS app.

Generate Firebase project configuration files

On the Firebase console, add a new iOS app or select an existing iOS app to your Firebase project. Must match iOS bundle IDyour local project . bundle IDFound in the General tab when you open ios/Runner.xcworkspace with Xcode bundle ID.

Download the GoogleService-Info.plist file for the Firebase application.

Install the Firebase configuration file

Next, you have to add the file to the project using Xcode (manual addition via the file system will not link the file to the project).

Open the project's ios/{projectName}.xcworkspace file with Xcode. In Xcode's left project navigator, right click on the Runner and select "Add Files"

Select the GoogleService-Info.plist file you downloaded and make sure the "Copy items if needed" checkbox is enabled.

Initialize FlutterFire

Before using any Firebase service, FlutterFire needs to be initialized (you can think of this process as FlutterFire "bootstrapping" itself). The initialization step is asynchronous, which means you need to prevent any FlutterFire related usage until initialization is complete.

Install

Before using any Firebase services, you must first install the firebase_core plugin, which is responsible for connecting your application to Firebase. Add the plugin to your pubspec.yaml file:

dependencies:
  flutter:
    sdk: flutter
  //add this
  firebase_core: "0.5.3"

Because firebase_coreonly a few basic functions are provided, we generally need other plug-ins. If you use other plug-ins of firebase, you don't need to import them firebase_core. Importing other plugins is automatically imported firebase_core, eg firebase_messaging.

initialization

To initialize FlutterFire, call the initializeApp method on the Firebase class:

await Firebase.initializeApp();

The method is asynchronous and returns a Future, so you need to make sure it's completed before you can use firebase related functions.

The example project shows how to use StatefulWidget to achieve this.

Improve iOS build time

Currently, the Firestore iOS SDK relies on about half a million lines of mostly C++ code that can take upwards of 5 minutes to build in XCode. To significantly reduce your build time, you can use a precompiled version by adding 1 line to your ios/Podfile in your Flutter project.

pod’FirebaseFirestore’,: git =>‘https://github.com/invertase/firestore-ios-sdk-frameworks.git’,: tag =>‘6.26.0’

Add this line in the target "Runner" in the target Podfile, for example:

# ...
target 'Runner' do
  pod 'FirebaseFirestore', :git => 'https://github.com/invertase/firestore-ios-sdk-frameworks.git', :tag => '6.26.0'
# ...
end

Also, make sure you have upgraded cocoapods to version 1.9.1 or higher:gem install cocoapods

See this issue for more information: https://github.com/FirebaseExtended/flutterfire/issues/2751

Guess you like

Origin blog.csdn.net/u011272795/article/details/110878313