How To Set Up Firebase with Flutter for iOS and Android Apps
source link: https://www.digitalocean.com/community/tutorials/flutter-firebase-setup
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Firebase is a great backend solution for anyone that wants to use authentication, databases, cloud functions, ads, and countless other features within an app.
In this article, you will create a Firebase project for iOS and Android platforms using Flutter.
Prerequisites
To complete this tutorial, you will need:
- A Google account to use Firebase.
- Developing for iOS will require XCode.
- To download and install Flutter.
- To download and install Android Studio and Visual Studio Code.
It is recommended to install plugins for your code editor:
This tutorial was verified with Flutter v2.0.6, Android SDK v31.0.2, and Android Studio v4.1.
Creating a New Flutter Project
This tutorial will require the creation of an example Flutter app.
Once you have your environment set up for Flutter, you can run the following to create a new application:
flutter create flutterfirebaseexample
Navigate to the new project directory:
cd flutterfirebaseexample
Using flutter create
will produce a demo application that will display the number of times a button is clicked.
Now that we’ve got a Flutter project up and running, we can add Firebase.
Creating a New Firebase Project
First, log in with your Google account to manage your Firebase projects. From within the Firebase dashboard, select the Create new project button and give it a name:
Next, we’re given the option to enable Google Analytics. This tutorial will not require Google Analytics, but you can also choose to add it to your project.
If you choose to use Google Analytics, you will need to review and accept the terms and conditions prior to project creation.
After pressing Continue, your project will be created and resources will be provisioned. You will then be directed to the dashboard for the new project.
Adding Android support
Registering the App
In order to add Android support to our Flutter application, select the Android logo from the dashboard. This brings us to the following screen:
The most important thing here is to match up the Android package name that you choose here with the one inside of our application.
The structure consists of at least two segments. A common pattern is to use a domain name, a company name, and the application name:
com.example.flutterfirebaseexample
Once you’ve decided on a name, open android/app/build.gradle
in your code editor and update the applicationId
to match the Android package name:
...
defaultConfig {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId 'com.example.flutterfirebaseexample'
...
}
...
You can skip the app nickname and debug signing keys at this stage. Select Register app to continue.
Downloading the Config File
The next step is to add the Firebase configuration file into our Flutter project. This is important as it contains the API keys and other critical information for Firebase to use.
Select Download google-services.json
from this page:
Next, move the google-services.json
file to the android/app
directory within the Flutter project.
Adding the Firebase SDK
We’ll now need to update our Gradle configuration to include the Google Services plugin.
Open android/build.gradle
in your code editor and modify it to include the following:
buildscript {
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
}
dependencies {
...
// Add this line
classpath 'com.google.gms:google-services:4.3.6'
}
}
allprojects {
...
repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository
...
}
}
Finally, update the app level file at android/app/build.gradle
to include the following:
apply plugin: 'com.android.application'
// Add this line
apply plugin: 'com.google.gms.google-services'
dependencies {
// Import the Firebase BoM
implementation platform('com.google.firebase:firebase-bom:28.0.0')
// Add the dependencies for any other desired Firebase products
// https://firebase.google.com/docs/android/setup#available-libraries
}
With this update, we’re essentially applying the Google Services plugin as well as looking at how other Flutter Firebase plugins can be activated such as Analytics.
From here, run your application on an Android device or simulator. If everything has worked correctly, you should get the following message in the dashboard:
Next up, let’s add iOS support!
Adding iOS Support
In order to add Firebase support for iOS, we have to follow a similar set of instructions.
Head back over to the dashboard and select Add app and then iOS icon to be navigated to the setup process.
Registering an App
Once again, we’ll need to add an “iOS Bundle ID”. It is possible to use the “Android package name” for consistency:
You’ll then need to make sure this matches up by opening the iOS project up in Xcode at ios/Runner/Runner.xcodeproj
and changing the Bundle identifier under General:
Click Register app to move to the next screen.
Downloading the Config File
In this step, we’ll need to download the configuration file and add this to our Xcode project.
Download GoogleService-Info.plist
and move this into the root of your Xcode project within Runner
:
Be sure to move this file within Xcode to create the proper file references.
There are additional steps for installing the Firebase SDK and adding initialization code, but they are not necessary for this tutorial.
That’s it!
Conclusion
In this article, you learned how to set up and ready our Flutter applications to be used with Firebase.
Flutter has official support for Firebase with the FlutterFire set of libraries.
In future articles, we’ll look at how to use Firebase features such as Cloud Firestore, Authentication, Analytics, and more with Flutter.
If you’d like to learn more about Flutter, check out our Flutter topic page for exercises and programming projects.
Recommend
-
40
With Google Firebase , one of the most powerful and versatile serverless platforms available today, you can effortlessly add essential features such as user management, crash re...
-
16
README.md A food ordering app built with flutter and firebase. [Still in development] Screenshots
-
17
Firebase Push Notifications in Flutter This article will show how to send push notifications in Flutter using Firebase. We will start with Android. Add Dependency Fo...
-
4
I/O Photo Booth A Photo Booth built with Flutter and Firebase for Google I/O 2021.
-
34
Flutter: CRUD operations using Firebase Cloud FirestoreCloud Firestore provides you an easily manageable pre-configured NoSQL database. It helps in storing...
-
11
Set up a Flutter app and Implement Google Sign-In Using FirebaseSeptember 18th 2021 new story8 W...
-
17
How to Update Markers in Flutter Maps from Firebase DatabaseA marker helps to easily identify locations, such as towns, buildings, or any points of interest on a map. But it may be difficult to manually update t...
-
11
Push Notification in Flutter using FirebaseLoaded: 0.68%00:03Remaining Time -27:23Push Notification in Flutter using Firebase
-
8
How to Use Google Sign-in With Firebase in Flutter?Loaded: 0.41%00:03Remaining Time -24:13How to Use Google Sign-in With Firebase in Flut...
-
1
Hi guys! In this blog post we will go over how to use the charts_flutter library(here is its pub.dev page) with Firebase when developing Flutter applications. More specifically, we will go over how to use the Time Series chart type to render...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK