Creating a New Firebase ProjectFirst, 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:
he 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:
android/app/build.gradle
...
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:
android/buiild.gradle
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:
android/app/build.gradle
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
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!
Initialize firebase in flutter
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'firebase_options.dart';
void main() => runApp(const MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
String get name => 'foo';
Future<void> initializeDefault() async {
FirebaseApp app = await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
print('Initialized default app $app');
}
Future<void> initializeDefaultFromAndroidResource() async {
if (defaultTargetPlatform != TargetPlatform.android || kIsWeb) {
print('Not running on Android, skipping');
return;
}
FirebaseApp app = await Firebase.initializeApp();
print('Initialized default app $app from Android resource');
}
Future<void> initializeSecondary() async {
FirebaseApp app = await Firebase.initializeApp(
name: name,
options: DefaultFirebaseOptions.currentPlatform,
);
print('Initialized $app');
}
void apps() {
final List<FirebaseApp> apps = Firebase.apps;
print('Currently initialized apps: $apps');
}
void options() {
final FirebaseApp app = Firebase.app();
final options = app.options;
print('Current options for app ${app.name}: $options');
}
Future<void> delete() async {
final FirebaseApp app = Firebase.app(name);
await app.delete();
print('App $name deleted');
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
title: const Text('Firebase Core example app'),
),
body: Padding(
padding: const EdgeInsets.all(20),
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
ElevatedButton(
onPressed: initializeDefault,
child: const Text('Initialize default app'),
),
if (defaultTargetPlatform == TargetPlatform.android && !kIsWeb)
ElevatedButton(
onPressed: initializeDefaultFromAndroidResource,
child: const Text(
'Initialize default app from Android resources',
),
),
ElevatedButton(
onPressed: initializeSecondary,
child: const Text('Initialize secondary app'),
),
ElevatedButton(
onPressed: apps,
child: const Text('List apps'),
),
ElevatedButton(
onPressed: options,
child: const Text('List default options'),
),
ElevatedButton(
onPressed: delete,
child: const Text('Delete secondary app'),
),
],
),
),
),
);
}
}
hello nice explainations