Sub topic 2
Connecting Your Flutter App to Firebase
Prerequisites and the Firebase connection
This chapter assumes that you know basic Flutter structure, async and await, and how to run a Flutter app on a device or emulator. You also need a Firebase project and a Flutter project.
Firebase Cloud Messaging (FCM) is a service that delivers push messages. Your Flutter app must be registered with Firebase before it can receive those messages. The registration connects each platform app to the same Firebase project.
A Firebase project can contain separate Android and iOS app registrations:
| Flutter target | Firebase registration | Platform identifier |
|---|---|---|
| Android | Android app | Application ID, such as `com.example.shop` |
| iOS | Apple app | Bundle ID, such as `com.example.shop` |
Add the Flutter packages
Add firebase_core to initialize Firebase and firebase_messaging to use FCM:
dependencies:
flutter:
sdk: flutter
firebase_core: ^3.15.0
firebase_messaging: ^15.2.0Run this command after editing pubspec.yaml:
flutter pub getThe package versions shown are examples. Use compatible current versions for your Flutter SDK.
Configure the platforms with FlutterFire CLI
The FlutterFire CLI creates the platform configuration for your Firebase project. Install and activate it once:
dart pub global activate flutterfire_cliFrom the root of your Flutter project, run:
flutterfire configureChoose an existing Firebase project or create one. Then select the Android and iOS platforms. The command generates lib/firebase_options.dart, which contains platform-specific Firebase options.
Keep this file with your application source. It does not contain a server credential, but treat your Firebase project configuration as part of your application setup. Do not put server keys or service-account JSON files in the Flutter app.
Initialize Firebase before using FCM
Initialize Flutter bindings before calling asynchronous code in main. Then initialize Firebase with the generated options:
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
import 'firebase_options.dart';
Future<void> main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(
const MaterialApp(
home: Scaffold(
body: Center(child: Text('Firebase is ready')),
),
),
);
}DefaultFirebaseOptions.currentPlatform selects the Android or iOS settings at runtime. The same Dart code can therefore initialize the correct Firebase app on both platforms.
After initialization, other Firebase packages can use the shared Firebase app:
final messaging = FirebaseMessaging.instance;Register background handlers and request notification permission only after this basic initialization is working. Keeping initialization in main also gives you one predictable place to handle startup failures.
Verify the setup
Run the app on each selected platform:
flutter runIf the app starts and displays Firebase is ready, initialization completed. A missing generated file, an incorrect application ID, or a platform configuration mismatch usually causes an error before the first widget appears.
Exercise
Why should Firebase.initializeApp run before accessing FirebaseMessaging.instance?
Show answer
Firebase Messaging depends on an initialized Firebase app so it knows which Firebase project and platform configuration to use.