• Home
  • Insight
  • Blog
  • Business
  • Entertainment
  • Health
  • Politics
  • Shop
    • Gift Shop
    • Value Shop
    • Store
    • Bargain Shop
    • Discount
  • Sports
  • Tech
  • Travel
  • USA
  • Video
  • World
    • Asia
    • Africa
    • South America
    • North America
    • Europe
    • Oceania
Thursday, July 30, 2026
No Result
View All Result
Subscribe Now
  • Home
  • Insight
  • Blog
  • Business
  • Entertainment
  • Health
  • Politics
  • Shop
    • Gift Shop
    • Value Shop
    • Store
    • Bargain Shop
    • Discount
  • Sports
  • Tech
  • Travel
  • USA
    TX Rep. Brandon Gill Mocks James Talarico Over Fauci Action Figure Post

    TX Rep. Brandon Gill Mocks James Talarico Over Fauci Action Figure Post

    NBA players are ‘being taught’ to flop

    NBA players are ‘being taught’ to flop

    Industry was warned for years about chemical ‘runaway’ dangers. Then came near-catastrophe in O.C.

    Industry was warned for years about chemical ‘runaway’ dangers. Then came near-catastrophe in O.C.

    Trump administration seeks government-wide NDAs to stop leaks : NPR

    Trump administration seeks government-wide NDAs to stop leaks : NPR

    Dog discharges shotgun inside truck, striking woman at Nebraska traffic light

    Dog discharges shotgun inside truck, striking woman at Nebraska traffic light

    Toshifumi Suzuki, Japan’s ‘God’ of Convenience Stores, Dies at 93

    Toshifumi Suzuki, Japan’s ‘God’ of Convenience Stores, Dies at 93

     Judge Sanctioned CoreCivic for Destroying Video in ICE Death Suit

     Judge Sanctioned CoreCivic for Destroying Video in ICE Death Suit

    Suspect dead after opening fire near White House security checkpoint, Secret Service says

    Suspect dead after opening fire near White House security checkpoint, Secret Service says

    Trump trashes Colbert’s high-rated finale as “no ratings”

    Trump trashes Colbert’s high-rated finale as “no ratings”

  • Video
  • World
    • Asia
    • Africa
    • South America
    • North America
    • Europe
    • Oceania
The Insight Post
  • Home
  • Insight
  • Blog
  • Business
  • Entertainment
  • Health
  • Politics
  • Shop
    • Gift Shop
    • Value Shop
    • Store
    • Bargain Shop
    • Discount
  • Sports
  • Tech
  • Travel
  • USA
    TX Rep. Brandon Gill Mocks James Talarico Over Fauci Action Figure Post

    TX Rep. Brandon Gill Mocks James Talarico Over Fauci Action Figure Post

    NBA players are ‘being taught’ to flop

    NBA players are ‘being taught’ to flop

    Industry was warned for years about chemical ‘runaway’ dangers. Then came near-catastrophe in O.C.

    Industry was warned for years about chemical ‘runaway’ dangers. Then came near-catastrophe in O.C.

    Trump administration seeks government-wide NDAs to stop leaks : NPR

    Trump administration seeks government-wide NDAs to stop leaks : NPR

    Dog discharges shotgun inside truck, striking woman at Nebraska traffic light

    Dog discharges shotgun inside truck, striking woman at Nebraska traffic light

    Toshifumi Suzuki, Japan’s ‘God’ of Convenience Stores, Dies at 93

    Toshifumi Suzuki, Japan’s ‘God’ of Convenience Stores, Dies at 93

     Judge Sanctioned CoreCivic for Destroying Video in ICE Death Suit

     Judge Sanctioned CoreCivic for Destroying Video in ICE Death Suit

    Suspect dead after opening fire near White House security checkpoint, Secret Service says

    Suspect dead after opening fire near White House security checkpoint, Secret Service says

    Trump trashes Colbert’s high-rated finale as “no ratings”

    Trump trashes Colbert’s high-rated finale as “no ratings”

  • Video
  • World
    • Asia
    • Africa
    • South America
    • North America
    • Europe
    • Oceania
No Result
View All Result
No Result
View All Result
Home Mobile

How to use Firebase emulator with Flutter

by Theinsightpost
September 1, 2022
in Mobile
0 0
0
How to use Firebase emulator with Flutter


This blog post is written by Hrishikesh Pathak

In this era of collaborative development in the cloud, local development makes it easy to iterate and build new features quickly. You can also make different versions of your projects very quickly to test new features in the local machine without affecting the production build.

What is Firebase Local Emulator Suite?

Firebase is a very popular Backend-as-a-Service (BaaS) platform offered by Google that caters to all your backend-related needs. It provides auth services, NoSQL databases, file storage, and much more.

If you use Firebase in your projects, you should have a constant connection to Firebase servers during development. You have to create different Firebase projects if you need multiple versions of your back end for testing purposes. These are some of the pain points of using Firebase for development in general.

Firebase Local Emulator Suite is an emulated version of Firebase that can be installed on your local device. When you want to test a Firebase feature, you can spin up a new emulator instantly on your machine and start interacting with it. Local Emulator Suite is great for fast iteration and prototyping of your Flutter app.

What you will learn in this tutorial

  1. Installing and starting the Firebase emulator for your Flutter project
  2. Connecting your Flutter app with Firebase Local Emulator Suite
  3. Adding Firebase Firestore to our default Flutter counter app
  4. Integration testing with Flutter and the Firebase emulator
  5. Configuring the Firebase emulator in Codemagic CI/CD

How to install the Firebase CLI on your machine

We need to install the Firebase CLI before installing the Firebase emulator. The easiest way to install the Firebase emulator is using npm (node package manager). If you have Node.js and npm installed on your device, you can install the Firebase CLI using this command.

npm install -g firebase-tools

If you don’t have Node.js installed, you can install Node.js from its official website. If you don’t want to install Node.js on your device, you can take a look at some alternative installation methods.

Sometimes, you can get an EACCES error while installing npm packages. Check out the npm documentation to resolve this issue.

You can check which Firebase version you have installed by running this command.

If you get a version number in return, then you have successfully installed the Firebase CLI. Keep in mind that you need version 8.14 or higher to install the Firebase emulator.

Now, inside your current Flutter project, initialize a new Firebase project.

Follow the terminal prompts and select the Firebase services you need for your project. After running the above command, you may be asked to authenticate yourself using your Firebase account. This authentication process helps create new Firebase projects based on your input. You can find the automatically created Firebase projects in your project dashboard.

Install and start the Firebase emulator on your machine

To install the Firebase emulator for your project, run this command at the root of your project.

Follow the on-screen prompts and select the emulator services you need for your project. You can customize your emulator settings later by editing the firebase.json file.

For this tutorial, we will only be using the Firebase Firestore emulator. So, select the Firestore option to follow along.

If you are creating a Firebase emulator for the first time on your machine, the Firebase CLI will download the binary file of the Firebase emulator and configure it for you. The downloaded binary is cached in your system, and subsequent emulators are created using the cached binary files.

To start the Firebase emulator, run the following command.

This command starts your Firebase emulator in the default ports. Firestore runs in port 8080 of your localhost, and you can access the emulator UI at localhost:4000.

Connect our Flutter app with the Firebase emulator

In this tutorial, we will modify the default Flutter counter app to demonstrate how the Firebase emulator works with Flutter. This will help you understand how to configure your project to use the Firebase emulator.

Initial setup

Go to your project root and install flutterfire_cli in your project. flutterfire_cli automatically generates all the Firebase-related config files for your project.

dart pub global activate flutterfire_cli

After installing flutterfire_cli, generate the Firebase configuration file for your Flutter application using flutterfire_cli. The following command creates a firebase_options.dart file inside your lib/ directory.

flutterfire configure

Now, add the firebase_core and cloud_firestore dependencies to your project.

flutter pub add firebase_core cloud_firestore

Connect the Flutter counter app with the Firebase emulator

Initialize firebase_core inside your main function.

import 'package:flutter/material.dart';
import "package:firebase_core/firebase_core.dart";
import 'firebase_options.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(const MyApp());
}

Inside the MyHomePage stateful widget, define the Firestore variable. If you are using an Android emulator, use 10.0.2.2 instead of localhost to connect with the Firebase emulator. For web builds, you can use localhost without any modifications.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'dart:io' show Platform;

// inside MyHomePage widget
late FirebaseFirestore firestore;
String host = Platform.isAndroid ? "10.0.2.2" : "localhost";
int count = 0;

Now, let’s connect to the local Firebase emulator inside the initState method of the widget.

  @override
  void initState() {
    firestore = FirebaseFirestore.instance;
    firestore.settings =
        const Settings(persistenceEnabled: false, sslEnabled: false);
    firestore.useFirestoreEmulator(host, 8080);
    super.initState();
  }

Firestore-powered counter app

To use Firestore features, inside the build method of the MyHomePage widget, add a stream builder to listen to new Firestore events and change our counter accordingly.

 StreamBuilder<DocumentSnapshot<Map<String, dynamic>>>(
              stream: firestore.collection("counter").doc("count").snapshots(),
              builder: (context, snapshot) {
                if (snapshot.hasError) {
                  return Text(
                    count.toString(),
                    style: Theme.of(context).textTheme.headline4,
                    semanticsLabel: "count",
                  );
                }
                return Text(
                  snapshot.data?.data()?["count"].toString() ?? "0",
                  style: Theme.of(context).textTheme.headline4,
                );
              },
            ),

Now, when someone changes the count document inside the counter collection, a new event is emitted and the change will be reflected on the screen.

Every time someone presses the floating action button, the count document is changed with a new count value.

 floatingActionButton: FloatingActionButton(
        onPressed: () async {
          count++;
          await firestore
              .collection("counter")
              .doc("count")
              .set({"count": count});
        },
        tooltip: "fab",
        child: const Icon(Icons.add),
      ),

Now, run this app using the flutter run command, and try out our new Firestore-powered counter app.

Integration testing with the Firebase emulator

Integration testing is done to see how an app’s individual components work together as a whole. It makes it easy to analyze the performance of the app.

In this tutorial, we will write a very simple integration test and see how to use the Firebase emulator for integration testing for Flutter apps.

Before starting, add these two development dependencies in the pubspec.yaml file.

dev_dependencies:
  integration_test:
    sdk: flutter
  flutter_test:
    sdk: flutter

Now, create a new directory integration_test inside your root directory. Put your integration test files inside this directory. Let’s create a file app_test.dart inside the integration_test directory to write our first integration test.

If you are a beginner and don’t know much about integration testing in Flutter, make sure to follow this Flutter cookbook on integration testing before proceeding. The flow of our test looks like this.

  1. Start the Firebase emulator
  2. Test if the counter starts with 0
  3. Find the floating action button and click it to increase the count
  4. Test if the counter is now at 1

This is a very simple integration test to demonstrate if the Firebase emulator is working. The code inside the app_test.dart file looks like this.

import 'package:flutter_test/flutter_test.dart';
import 'package:integration_test/integration_test.dart';
import 'package:flutter_firebase_emulator/main.dart' as app;

void main() {
  final IntegrationTestWidgetsFlutterBinding binding =
      IntegrationTestWidgetsFlutterBinding.ensureInitialized();

  testWidgets("Tapping on floating action button and verify counter",
      (tester) async {
    app.main();
    await tester.pumpAndSettle();

    expect(find.text('0'), findsOneWidget);

    final Finder fab = find.byTooltip("fab");

    await tester.tap(fab);

    await tester.pumpAndSettle();

    expect(find.text("1"), findsOneWidget);
  });
}

Before starting the test, make sure to start the Firebase emulator by running the firebase emulator:start command.

You should also have an emulator running for integration testing. To get a list of your available emulators, run the flutter emulators command.

Select one of these emulators and start it using its emulator ID.

flutter emulators --launch <emulator ID>

Now, you need to run the integration test command. This command launches your application in an emulator and carries out all the tests you have defined in the test file.

flutter test integration_test/app_test.dart

If everything goes well, you should see the line “All tests passed!” printed on your terminal. Congratulations! ?

Run the Firebase emulator with Codemagic CI/CD

Before starting this section, create a new GitHub repository and upload the project to connect with the Codemagic CI/CD pipeline. On your Codemagic dashboard, add a new Flutter application using the aforementioned GitHub repository.

In this tutorial, I am using Codemagic’s visual editor for Flutter. In your terminal, run the firebase login:ci command to get an authentication token that you can use inside your CI/CD pipeline.

Inside the environment variable section of the Codemagic editor, add the Firebase token we get from the login:ci command.

In the Dependency caching tab, click on Enable dependency caching and enter ~/.cache/firebase/emulators as the path. The Firebase CLI stores the downloaded emulators in this location. Setting up the cache helps to reduce the build time.

Now, click on the gear icon above the tests tab. In the post-clone script, we’ll write a bash script to install the Firebase CLI and start the Firebase emulator to do our testing.

#!/bin/bash

# installing the Firebase CLI
curl -sL https://firebase.tools | bash

# start the emulator
firebase emulators:start &

Do you notice the & symbol after the firebase emulator:start command? This symbol runs the Firebase emulator in the background and frees up the terminal to run the next processes. Otherwise, our build would be stuck at this position.

Now you can run your integration tests in Codemagic CI/CD using Firebase emulators. You can also connect to Firebase Test Lab to test your Flutter application on a real device. To learn more, read our in-depth article on Flutter integration testing with Firebase Test Lab and Codemagic CI/CD.

Conclusion

If you like this tutorial on using the Firebase emulator with Flutter, please share it with your peers. If you have any questions, feel free to ask me. My name is Hrishikesh Pathak, and my Twitter handle is @hrishikshpathak. Have a great day.





Source link

ShareTweetSend
Previous Post

Stoughton art gallery opens exhibit to raise money for Ukraine | News

Next Post

“serious human rights violations” in Xinjiang says UN – europeantimes.news

Related News

Announcing Codemagic Patch: an open-source CodePush rebuild for React Native
Mobile

Announcing Codemagic Patch: an open-source CodePush rebuild for React Native

July 30, 2026
React Native OTA Updates: What You Can (and Can’t) Deploy Over the Air
Mobile

React Native OTA Updates: What You Can (and Can’t) Deploy Over the Air

May 28, 2026
Your phone drawer hides an app designed to “exploit developing brains”
Mobile

Your phone drawer hides an app designed to “exploit developing brains”

May 27, 2026
iPhone 16 has a secret charging upgrade Apple didn’t even mention
Mobile

iPhone 16 has a secret charging upgrade Apple didn’t even mention

May 26, 2026
Next Post
“serious human rights violations” in Xinjiang says UN – europeantimes.news

"serious human rights violations" in Xinjiang says UN – europeantimes.news

Discussion about this post

Subscribe To Our Newsletters

    Customer Support


    1251 Wilcrest Drive
    Houston, Texas
    77042 USA
    Call-832.795.1420
    e-mail – news@theinsightpost.com

    Subscribe To Our Newsletters

      Categories

      • Africa
      • Africa-East
      • African Sports
      • American Sports
      • Arts
      • Asia
      • Australia
      • Business
      • Business Asia
      • Business- Africa
      • Canada
      • Defense
      • Education
      • Egypt
      • Energy
      • Entertainment
      • Europe
      • European Soccer
      • Finance
      • Germany
      • Ghana
      • Health
      • Insight
      • International
      • Investing
      • Japan
      • Latest Headlines
      • Life & Living
      • Markets
      • Mobile
      • Movies
      • New Zealand
      • Nigeria
      • Politics
      • Scholarships
      • Science
      • South Africa
      • South America
      • Sports
      • Tech
      • Travel
      • UK
      • USA
      • Weather
      • World
      No Result
      View All Result

      Recent News

      Free Cloud Computing Course | AWS Re/Start Program 

      Free Cloud Computing Course | AWS Re/Start Program 

      July 30, 2026
      Thirty Years Later

      Thirty Years Later

      July 30, 2026
      Announcing Codemagic Patch: an open-source CodePush rebuild for React Native

      Announcing Codemagic Patch: an open-source CodePush rebuild for React Native

      July 30, 2026
      Following Expansion in China, Regional Private Equity Faces New Scrutiny in Southeast Asia – The Diplomat

      Following Expansion in China, Regional Private Equity Faces New Scrutiny in Southeast Asia – The Diplomat

      July 30, 2026
      • Home
      • Advertise With Us
      • About Us
      • Corporate
      • Consumer Rewards
      • Forum
      • Privacy Policy
      • Social Trends

      Theinsightpost ©2026 | All Rights Reserved. Theinsightpost is an Elnegy LLC company, registered in Texas, USA

      Welcome Back!

      Login to your account below

      Forgotten Password?

      Retrieve your password

      Please enter your username or email address to reset your password.

      Log In

      Add New Playlist

      We are using cookies to give you the best experience on our website.

      You can find out more about which cookies we are using or switch them off in .

      No Result
      View All Result
      • Home
      • Insight
      • Blog
      • Business
      • Entertainment
      • Health
      • Politics
      • Shop
        • Gift Shop
        • Value Shop
        • Store
        • Bargain Shop
        • Discount
      • Sports
      • Tech
      • Travel
      • USA
      • Video
      • World
        • Asia
        • Africa
        • South America
        • North America
        • Europe
        • Oceania

      Theinsightpost ©2026 | All Rights Reserved. Theinsightpost is an Elnegy LLC company, registered in Texas, USA

      The Insight Post
      Powered by  GDPR Cookie Compliance
      Privacy Overview

      This website uses cookies so that we can provide you with the best user experience possible. Cookie information is stored in your browser and performs functions such as recognising you when you return to our website and helping our team to understand which sections of the website you find most interesting and useful.

      Strictly Necessary Cookies

      Strictly Necessary Cookie should be enabled at all times so that we can save your preferences for cookie settings.

      Cookie Policy

      More information about our Cookie Policy