Log into Firebase using your Google account by running the following
command:
firebaselogin
Install the FlutterFire CLI by running the following command from any
directory:
dartpubglobalactivateflutterfire_cli
Step 2: Configure your apps to use Firebase
Use the FlutterFire CLI to configure your Flutter apps to connect to Firebase.
From your Flutter project directory, run the following command to start the
app configuration workflow:
flutterfireconfigure
What does thisflutterfire configureworkflow do?
Theflutterfire configureworkflow does the following:
Asks you to select the platforms (iOS, Android, Web) supported in your
Flutter app. For each selected platform, the FlutterFire CLI creates a new
Firebase app in your Firebase project.
You can select either to use an existing Firebase project or to create a
new Firebase project. If you already have apps registered in an existing
Firebase project, the FlutterFire CLI will attempt to match them based on
your current Flutter project configuration.
Creates a Firebase configuration file (firebase_options.dart) and adds it
to yourlib/directory.
(forCrashlyticsorPerformance Monitoringon Android)Adds the required
product-specific Gradle plugins to your Flutter app.
Step 3: Initialize Firebase in your app
From your Flutter project directory, run the following command to install
the core plugin:
flutterpubaddfirebase_core
From your Flutter project directory, run the following command to ensure
that your Flutter app's Firebase configuration is up-to-date:
flutterfireconfigure
In yourlib/main.dartfile, import the Firebase core plugin and the
configuration file you generated earlier:
If you would rather use a demo project, you can start theFirebase Emulatorand
in yourlib/main.dartfile initialize Firebase usingdemoProjectId(it should start withdemo-):
You access Firebase in your Flutter app through the variousFirebase Flutter plugins, one for each Firebase product
(for example:Cloud Firestore,Authentication,Analytics, etc.).
Since Flutter is a multi-platform framework, each Firebase plugin is applicable
for Apple, Android, and web platforms. So, if you add any Firebase plugin to
your Flutter app, it will be used by the Apple, Android, and web versions of
your app.
Here's how to add a Firebase Flutter plugin:
From your Flutter project directory, run the following command:
flutter pub addPLUGIN_NAME
From your Flutter project directory, run the following command:
flutterfireconfigure
Running this command ensures that your Flutter app's Firebase configuration
is up-to-date and, forCrashlyticsandPerformance Monitoringon Android, adds the
required Gradle plugins to your app.
Once complete, rebuild your Flutter project:
flutterrun
You're all set! Your Flutter apps are registered and configured to use Firebase.
Like all packages, thefirebase_analyticsplugin comes with anexample program.
Open a Flutter app that you've already configured to use Firebase (see
instructions on this page).
Access thelibdirectory of the app, then delete the existingmain.dartfile.
From theGoogle Analyticsexample program repository,
copy-paste the following two files into your app'slibdirectory:
main.dart
tabs_page.dart
Run your Flutter app.
Go to your app's Firebase project in theFirebaseconsole, then clickAnalyticsin the left-nav.
ClickDashboard.
IfAnalyticsis working properly, the dashboard shows an active user
in the "Users active in the last 30 minutes" panel (this might take
time to populate this panel).
ClickDebugView. Enable the feature to
see all the events generated by the example program.
For more information about setting upAnalytics, visit the getting started
guides foriOS+,Android, andweb.