Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

How to integrate Flutter with Firebase

bhardwaju
Staff

Authored by:

  • Utkarsh Bhardwaj (@bhardwaju), Cloud Migration Consultant, Google
  • Shivank Awasthi (@shivankawasthi), Strategic Cloud Engineer, Google

Video by:

  • Amit Dutta, (@amitdutta), Strategic Cloud Engineer, Google

In this article, we'll discuss how to integrate Flutter with Firebase. We'll use the Firebase plugin to connect our Flutter application to Firebase.

If you have any questions, please leave a comment below! 

To get started, we need to create a new Flutter project. This can be done by running the following command: 

 

flutter create my_app

 

This command will create a new Flutter project called my_app

Next, we need to add the Firebase plugin to our project. This plugin will allow us to connect our Flutter application to Firebase. 

To add the Firebase plugin, we need to open the pubspec.yaml file in the root directory of our Flutter project. This file will contain the information about our project, such as its name, version, and dependencies.

The following is an example of a pubspec.yaml file:

 

name: my_app
version: 1.0.0
description: A Flutter application for doing cool things.

dependencies:
    flutter: 2.0.0
    google_services: 1.0.0

 

Once we've added the Firebase plugin to our project, we need to run the Flutter packages get command to install the plugin. This command will download and install the plugin from the Dart Pub repository.

Now, we're ready to connect our Flutter application to Firebase. We can do this by adding the following code to the main.dart file:

 

import 'package:flutter/material.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter/painting.dart';
import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Flutter Demo'),
        ),
        body: Center(
          child: FirebaseAuth(),
        ),
      ),
    );
  }
}

class FirebaseAuth extends StatefulWidget {
  @override
  State createState() => _FirebaseAuthState();
}

class _FirebaseAuthState extends State<FirebaseAuth> {
  @override
  void initState() {
    super.initState();

    // Set up Firebase authentication.
    FlutterFire.initializeApp(this.context);
  }

  @override
  void dispose() {
    super.dispose();

    // Clear Firebase authentication.
    FlutterFire.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Firebase Demo'),
      ),
      body: Center(
        child: Button(
          onPressed: () async {
            // Sign in the user.
            await FlutterFire.signInWithEmailAndPassword(email: 'user@example.com', password: 'password');
          },
          child: Text('Sign in'),
        ),
      ),
    );
  }
}

 

This code will connect our Flutter application to Firebase. We can then use Firebase to authenticate users and store data.

That's it! We've successfully integrated Flutter with Firebase.

Disclaimer: All the sample codes in this document are point in time information which may change depending on the time you are reading this blog.

3 0 1,535
Authors