Bird
Raised Fist0
Fluttermobile~20 mins

Firebase setup for Flutter - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Firebase Initialization Screen
This screen initializes Firebase when the app starts and shows a simple message once Firebase is ready.
Target UI
-------------------------
| Firebase Init Screen   |
|                       |
| [Initializing...]      |
|                       |
|                       |
-------------------------
Add Firebase core package to the project
Initialize Firebase asynchronously when the app starts
Show 'Initializing...' text while Firebase is loading
Show 'Firebase Initialized!' text after successful initialization
Handle and show error message if Firebase fails to initialize
Starter Code
Flutter
import 'package:flutter/material.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: FirebaseInitScreen(),
    );
  }
}

class FirebaseInitScreen extends StatefulWidget {
  const FirebaseInitScreen({super.key});

  @override
  State<FirebaseInitScreen> createState() => _FirebaseInitScreenState();
}

class _FirebaseInitScreenState extends State<FirebaseInitScreen> {
  // TODO: Add Firebase initialization logic here

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Firebase Init Screen')),
      body: const Center(
        child: Text('Initializing...'),
      ),
    );
  }
}
Task 1
Task 2
Task 3
Task 4
Solution
Flutter
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: FirebaseInitScreen(),
    );
  }
}

class FirebaseInitScreen extends StatefulWidget {
  const FirebaseInitScreen({super.key});

  @override
  State<FirebaseInitScreen> createState() => _FirebaseInitScreenState();
}

class _FirebaseInitScreenState extends State<FirebaseInitScreen> {
  late Future<FirebaseApp> _firebaseInitialization;
  String _statusMessage = 'Initializing...';

  @override
  void initState() {
    super.initState();
    _firebaseInitialization = Firebase.initializeApp();
    _firebaseInitialization.then((_) {
      setState(() {
        _statusMessage = 'Firebase Initialized!';
      });
    }).catchError((error) {
      setState(() {
        _statusMessage = 'Error initializing Firebase: $error';
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('Firebase Init Screen')),
      body: Center(
        child: Text(_statusMessage),
      ),
    );
  }
}

We import the firebase_core package to access Firebase initialization.

In initState, we start initializing Firebase asynchronously with Firebase.initializeApp(). We store the future in _firebaseInitialization.

We use then and catchError on the future to update a state variable _statusMessage depending on success or failure.

The UI shows the current status message in the center of the screen.

This approach ensures the app waits for Firebase setup and informs the user about the progress or errors.

Final Result
Completed Screen
-------------------------
| Firebase Init Screen   |
|                       |
| Firebase Initialized!  |
|                       |
|                       |
-------------------------
When the app starts, the screen shows 'Initializing...'
After Firebase finishes initializing, the text changes to 'Firebase Initialized!'
If initialization fails, the text shows an error message
Stretch Goal
Add a retry button that appears if Firebase initialization fails, allowing the user to try initializing again.
💡 Hint
Use a button that calls the Firebase initialization function again and updates the state accordingly.

Practice

(1/5)
1. What is the first step to connect your Flutter app to Firebase?
easy
A. Add Firebase dependencies after running the app
B. Write Flutter UI code
C. Create a Firebase project in the Firebase console
D. Run the app without any setup

Solution

  1. Step 1: Access Firebase Console

    You must first create a Firebase project in the Firebase console to get started.
  2. Step 2: Prepare for Flutter integration

    After creating the project, you can download config files and add dependencies.
  3. Final Answer:

    Create a Firebase project in the Firebase console -> Option C
  4. Quick Check:

    First step = Create Firebase project [OK]
Hint: Start by creating a Firebase project online [OK]
Common Mistakes:
  • Skipping Firebase project creation
  • Trying to run app before setup
  • Adding dependencies too late
2. Which file must be added to your Flutter Android app after creating a Firebase project?
easy
A. google-services.json
B. GoogleService-Info.plist
C. firebase-config.xml
D. firebase-settings.json

Solution

  1. Step 1: Identify Android config file

    For Android apps, Firebase requires the google-services.json file.
  2. Step 2: Add file to Android app folder

    This file must be placed in the android/app directory of your Flutter project.
  3. Final Answer:

    google-services.json -> Option A
  4. Quick Check:

    Android config file = google-services.json [OK]
Hint: Android uses google-services.json config file [OK]
Common Mistakes:
  • Using iOS plist file for Android
  • Misnaming the config file
  • Placing file in wrong folder
3. What will happen if you forget to call Firebase.initializeApp() before runApp() in Flutter?
medium
A. App runs normally without Firebase
B. App crashes on build time
C. Firebase features work partially
D. App throws an error at runtime

Solution

  1. Step 1: Understand Firebase initialization

    Firebase must be initialized before the app runs to set up services properly.
  2. Step 2: Consequence of missing initialization

    If Firebase.initializeApp() is not called first, the app will throw a runtime error when Firebase is accessed.
  3. Final Answer:

    App throws an error at runtime -> Option D
  4. Quick Check:

    Missing initializeApp() = runtime error [OK]
Hint: Always initialize Firebase before runApp() [OK]
Common Mistakes:
  • Calling initializeApp() after runApp()
  • Assuming app runs without initialization
  • Ignoring runtime errors
4. You added google-services.json but forgot to add the Firebase plugin in build.gradle. What is the likely result?
medium
A. Firebase services work fine anyway
B. Build fails with plugin not found error
C. App runs but Firebase features are disabled
D. App crashes only on iOS devices

Solution

  1. Step 1: Role of Firebase plugin in build.gradle

    The Firebase plugin is required to process the config file and link Firebase SDK during build.
  2. Step 2: Effect of missing plugin

    Without the plugin, the build system cannot find Firebase tasks, causing a build failure.
  3. Final Answer:

    Build fails with plugin not found error -> Option B
  4. Quick Check:

    Missing plugin = build failure [OK]
Hint: Add Firebase plugin in build.gradle to avoid build errors [OK]
Common Mistakes:
  • Only adding config file without plugin
  • Ignoring build errors
  • Assuming plugin is optional
5. You want to support both Android and iOS in your Flutter app with Firebase. Which steps are required?
hard
A. Add google-services.json for Android and GoogleService-Info.plist for iOS, then initialize Firebase in main
B. Add only google-services.json and initialize Firebase in main
C. Add only GoogleService-Info.plist and initialize Firebase in main
D. Initialize Firebase in main without adding any config files

Solution

  1. Step 1: Add platform-specific config files

    Android requires google-services.json and iOS requires GoogleService-Info.plist to connect to Firebase.
  2. Step 2: Initialize Firebase in main function

    Call Firebase.initializeApp() before runApp() to set up Firebase services.
  3. Final Answer:

    Add google-services.json for Android and GoogleService-Info.plist for iOS, then initialize Firebase in main -> Option A
  4. Quick Check:

    Both config files + initialize = correct setup [OK]
Hint: Add both config files and initialize Firebase before runApp() [OK]
Common Mistakes:
  • Adding only one platform's config file
  • Forgetting to initialize Firebase
  • Assuming config files auto-load
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard