Integrating Firebase with your Flutter app

Firebase is a powerful platform that offers various services for building scalable and feature-rich applications. In this guide, we will walk through the process of adding Firebase to your Flutter project.

05.09.2023 — Edu Molins — 2 min read
Flutter firebase b

Step 1: Create a Firebase Project

  1. Go to the Firebase Console and click on "Add project."
  2. Follow the prompts to set up your project. Once done, click on "Continue" and then "Create project."

Step 2: Add your Flutter App to Firebase

  1. After creating the project, click on "Add app" and select the Flutter icon.
  2. Register your app by providing a nickname and package name (make sure it matches your Flutter project).
  3. Download the google-services.json file and place it in the android/app directory.
  4. For iOS, click on "Register App" and follow the prompts. Download the GoogleService-Info.plist file and add it to the ios/Runner directory of your Flutter project.

Step 3: Configure Firebase in your Flutter Project

  1. Open your pubspec.yaml file and add the Firebase dependencies:

    dependencies: 
      firebase_core: ^latest_version 
      firebase_auth: ^latest_version 
      cloud_firestore: ^latest_version
  2. Run flutter pub get in your terminal to install the new dependencies.

Step 4: Initialize Firebase in your Flutter App

 

  1. Import the Firebase packages in your main.dart:

    import 'package:firebase_core/firebase_core.dart';
  2. Initialize Firebase in the main function:

    void main() async { 
       WidgetsFlutterBinding.ensureInitialized(); 
       await Firebase.initializeApp(); 
       runApp(MyApp()); 
    }

 

Step 5: Use Firebase Services in your Flutter App

Authentication

 

  1. Import the necessary packages:

    import 'package:firebase_auth/firebase_auth.dart';
  2. Implement authentication:

    final FirebaseAuth _auth = FirebaseAuth.instance; 
    
    Future<void> signInWithGoogle() async { 
      try { 
        // Implement Google Sign-In logic here 
        UserCredential userCredential = await _auth.signInWithGoogle(); 
        print("Signed in: ${userCredential.user.displayName}"); 
      } catch (e) { 
        print("Error during Google Sign-In: $e");
      } 
    }

 

Firestore Database

  1. Import the necessary packages:

    import 'package:cloud_firestore/cloud_firestore.dart';
  2. Use Firestore in your code:

    final FirebaseFirestore _firestore = FirebaseFirestore.instance; 
    
    Future<void> addUserToFirestore(String username, String email) { 
      return _firestore.collection('users').add({ 
        'username': username, 
        'email': email, 
      });
    }

Step 6: Test your Firebase Integration

Run your Flutter app, and ensure that Firebase is initialized without any errors. Test the authentication and Firestore features to confirm their functionality.

Congratulations! You've successfully integrated Firebase into your Flutter project. This sets the foundation for incorporating various Firebase services into your app to enhance its functionality.

⚡️ Get the Kit Digital grant to digitize your business. Read more.