Flutter integration Firebase
FlutterFire is a set of Flutter plug-ins that connect Flutter applications to Firebase.
First, create a Firebase project
Just follow the Firebase process
Android installed
Before you can use FlutterFire on Android, you must first connect to the Firebase project using an Android application.
Generate the Firebase project configuration file
On the Firebase console, add a new Android application or select an existing Android application for your Firebase project.
After registering your Android application, download the configuration file (called Google-services.json) from the Firebase console. Add this file to the Android/app directory in the Flutter project.
Install the Firebase configuration file
In order to allow Firebase to use the configuration on Android, the Google-Services plug-in must be applied to the project. This requires the modification of two files in the Android/directory.
First, add the Google-services plugin as a dependency in the Android/build.gradle file:
buildscript {
dependencies {
// ... other dependencies
classpath 'com. Google. GMS: Google - services: 4.3.3'}}Copy the code
Finally, in the/android/app/build. Gradle file to add the following to perform the plugin:
apply plugin: 'com.android.application'
//add this
apply plugin: 'com.google.gms.google-services'
Copy the code
For Android build
Due to the number of classes in some Of the Firebase SDKS (especially Firestore), it may take you beyond the 64K method limit on the Android build system, and you may receive Error message Error: number of method references when merging the dex file. The value in the dex file cannot exceed 64K.
If this error does occur, we recommend enabling Multidex for Android.
Enable Multidex
If your app is only for Android 21 or later (minSdkVersion), Multidex is enabled by default and you do not 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 changes to the application project:
Open/android/app/build. Gradle file. Add the multidex module under the dependency 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'
}
Copy the code
Please visit the Official Android documentation for more information.
IOS installed
Before you can use FlutterFire on iOS, you must first connect to the Firebase project using the iOS application.
Generate the Firebase project configuration file
On the Firebase console, add a new iOS app or select an existing iOS app for your Firebase project. The iOS bundle ID must match your local project bundle ID. When you open the ios/Runner. Xcworkspace with Xcode, you can find the bundle ID in the general TAB.
Download the Firebase application’s Googleservice-info.plist file.
Install the Firebase configuration file
Next, you must add the file to the project using Xcode (manually adding the file through the file system does not link the file to the project).
Use Xcode to open the ios / {projectName}.xcworkspace file for the project. In the left project navigation of Xcode, right-click Runner and choose “Add File”,
Select the Googleservice-info.plist file that you downloaded and make sure that the Copy Items if needed check box is enabled.
Initialize FlutterFire
FlutterFire needs to be initialized before any Firebase service can be used (you can think of this as the FlutterFire “boot” itself). The initialization step is asynchronous, which means that you need to prevent any Flutterfire-related use until the initialization is complete.
The installation
Before you can use any Firebase service, you must first install the FireBase_core plug-in, which connects your application to Firebase. To add the plug-in to your pubspec.yaml file:
dependencies:
flutter:
sdk: flutter
//add this
firebase_core: "0.5.3"
Copy the code
Since FireBase_core provides only a few basic functions, you’ll usually need other plug-ins. If you’re using fireBase’s other plug-ins, you don’t need to import FireBase_core. Introducing other plug-ins, such as Firebase_Messaging, automatically introduces FireBase_core.
Initialize the
To initialize FlutterFire, call the initializeApp method on the Firebase class:
await Firebase.initializeApp();
Copy the code
This method is asynchronous and returns a Future, so you need to make sure it’s done before you can use FireBase related functionality.
The sample project shows how to do this using the StatefulWidget.
Reduce iOS build time
Currently, the Firestore iOS SDK relies on most C ++ code of about 500,000 lines, which can take more than five minutes to build in XCode. To significantly reduce build time, you can use the precompiled version by adding 1 line to the ios/Podfile in the Flutter project.
Pod ‘FirebaseFirestore’ : git = > ‘github.com/invertase/f… ‘: tag = >’ 6.26.0 ‘
Add this line to 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
Copy the code
Also, make sure you have upgraded Cocoapods to version 1.9.1 or higher: gem Install Cocoapods
For more information, see the following questions: github.com/FirebaseExt…