Setting up Firebase
Prerequisites
- Flutter SDK installed and configured
- A Google account
- Flutter project created and running
- Firebase CLI installed
Installation Steps
1. Create a Firebase Project
- Go to the Firebase Console
- Click “Create a project” or “Add project”
- Enter your project name
- Follow the setup wizard to complete project creation
2. Install Required CLI Tools
# Install the FlutterFire CLIdart pub global activate flutterfire_cli
3. Configure Flutter Project
- Add the following dependencies to your
pubspec.yaml
:
dependencies: firebase_core: ^latest_version # Add other Firebase packages as needed: # firebase_auth: ^latest_version # cloud_firestore: ^latest_version # firebase_storage: ^latest_version
- Run flutter pub get:
flutter pub get
4. Initialize Firebase in Your Project
- Run the FlutterFire configure command. Be sure to specify the flavour (dev/stg/prod):
flutterfire configure -p flex-storefront-dev -i com.base1.flex-storefront.dev -a com.base1.flex_storefront.dev --out lib/firebase_options_dev.dart
-
-p
Sets the Firebase Project ID to use. -
-i
The bundle identifier of your iOS app, e.g. “com.example.app”. -
-a
The package name of your Android app, e.g. “com.example.app”. -
--out
Specifies the path & file name for the generated file.More information on flags may be found here.
- Select your created Firebase project
- This will automatically:
- Register your apps in Firebase console
- Download configuration files
- Add platform-specific configuration
5. Update Your Flutter Code
- Initialize Firebase in your main.dart:
import 'package:firebase_core/firebase_core.dart';import 'firebase_options.dart';
void main() async { WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); runApp(MyApp());}
Platform-Specific Setup
Android Setup
- Ensure your app’s
minSdkVersion
is at least 19 inandroid/app/build.gradle
:
android { defaultConfig { minSdkVersion 19 // ... }}
iOS Setup
- Update your iOS deployment target in Xcode to at least iOS 11.0
- Add the GoogleService-Info.plist to your Runner target
- Update your iOS permissions in Info.plist if needed
Verification
To verify your Firebase setup:
- Run your app
- Check the console for successful Firebase initialization
- Try implementing a basic Firebase feature (like Analytics)
Common Issues and Troubleshooting
Android Issues
- Gradle sync failures: Check your project-level build.gradle for Google services plugin
- Missing google-services.json: Ensure the file is in android/app/
iOS Issues
- Missing GoogleService-Info.plist
- Incorrect Bundle ID
- CocoaPods installation issues
Next Steps
After successful installation, you can:
- Add Firebase Authentication
- Set up Cloud Firestore
- Implement Firebase Cloud Messaging
- Add Firebase Analytics
Additional Resources
Walkthrough Video
Follow along as the FLEX Team sets up Firebase and Crashlytics in a Storefront App.