Skip to content

Firebase setup guide #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 81 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1 +1,81 @@
# Flutter_Firebase
# Flutter_Firebase Setup Guide

This guide will help you set up your **Flutter** project with **Firebase** for the first time. Follow the steps below to get started:

### Step 1: Create Project in VSCode and Set up Firebase Console

1. **Create a new Flutter project in VSCode**:
- Open VSCode and create a new Flutter project by running the following command in the terminal:
```bash
flutter create flutter_firebase_project
```

2. **Set up Firebase Console**:
- Go to the [Firebase Console](https://console.firebase.google.com/).
- Create a new Firebase project in the console.

![Step 1](media/step1.png)

### Step 2: Set Project Name in Firebase Console

- After creating the project in the Firebase Console, make sure to set the project name according to your desired naming convention.

![Step 2](media/step2.png)

### Step 3: Choose Default Account

- In the Firebase Console, choose your **default account** for Firebase services.

![Step 4](media/step4.png)

### Step 4: Select Flutter in "Get Started by Adding Firebase to Your App"

1. In the Firebase Console, navigate to the **"Get Started by Adding Firebase to Your App"** section.

2. Select **Flutter** as your development platform.

![Step 5](media/step5.png)

### Step 5: Follow Firebase Console's Flow Guide Step by Step

- Firebase provides a step-by-step guide to configure Firebase with your Flutter app. Follow these steps carefully to ensure proper setup.

- **Important**: Firebase recommends using **npm** to install the Firebase CLI to help with configuration.

1. **Install Firebase CLI**:

- Open your terminal in VSCode and run the following command to install Firebase CLI globally:

```bash
npm install -g firebase-tools
```

- The Firebase CLI will help you manage and deploy Firebase services.

![Step 6](media/step6.png)

2. **Connect Firebase to Your Flutter Project**:

- In the Firebase Console, follow the instructions to add Firebase to your Flutter project.

- You will be provided with a configuration file (google-services.json for Android, GoogleService-Info.plist for iOS), rest assured if you follow the instructions correctly, those files will be automatically added to your project add to your project.

![Step 6-2](media/step6-2.png)

3. **Modify `main.dart` to Initialize Firebase**:

- In your Flutter project, modify the `main.dart` file to initialize Firebase. Add the following code at the beginning of your `main.dart`

- This code ensures that Firebase is initialized before your app starts running.

![Step 6-3](media/step6-3.png)

### Step 6: Success!

- After completing the setup, you will see a confirmation screen indicating that Firebase has been successfully integrated into your Flutter project.

![Step 7](media/step7.png)

---

Now you are ready to start using Firebase with your Flutter project. You can proceed with adding Firebase services such as Authentication, Firestore, and more.
3 changes: 3 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
plugins {
id "com.android.application"
// START: FlutterFire Configuration
id 'com.google.gms.google-services'
// END: FlutterFire Configuration
id "kotlin-android"
// The Flutter Gradle Plugin must be applied after the Android and Kotlin Gradle plugins.
id "dev.flutter.flutter-gradle-plugin"
Expand Down
3 changes: 3 additions & 0 deletions android/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ pluginManagement {
plugins {
id "dev.flutter.flutter-plugin-loader" version "1.0.0"
id "com.android.application" version "8.1.0" apply false
// START: FlutterFire Configuration
id "com.google.gms.google-services" version "4.3.15" apply false
// END: FlutterFire Configuration
id "org.jetbrains.kotlin.android" version "1.8.22" apply false
}

Expand Down
145 changes: 51 additions & 94 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,125 +1,82 @@
import 'package:flutter/material.dart';
import 'package:flutter_firebase/firebase_options.dart';
import 'package:firebase_core/firebase_core.dart';

void main() {
runApp(const MyApp());
void main() async {
WidgetsFlutterBinding.ensureInitialized();
try {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
runApp(const MyApp());
} catch (e) {
runApp(MyApp(errorMessage: 'Failed to connect to Firebase: $e'));
}
}

class MyApp extends StatelessWidget {
const MyApp({super.key});
final String? errorMessage;

const MyApp({super.key, this.errorMessage});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
title: 'Flutter Firebase Demo',
theme: ThemeData(
// This is the theme of your application.
//
// TRY THIS: Try running your application with "flutter run". You'll see
// the application has a purple toolbar. Then, without quitting the app,
// try changing the seedColor in the colorScheme below to Colors.green
// and then invoke "hot reload" (save your changes or press the "hot
// reload" button in a Flutter-supported IDE, or press "r" if you used
// the command line to start the app).
//
// Notice that the counter didn't reset back to zero; the application
// state is not lost during the reload. To reset the state, use hot
// restart instead.
//
// This works for code too, not just values: Most code changes can be
// tested with just a hot reload.
colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
colorScheme: ColorScheme.fromSeed(seedColor: Colors.orange),
useMaterial3: true,
),
home: const MyHomePage(title: 'Flutter Demo Home Page'),
home: MyHomePage(errorMessage: errorMessage),
);
}
}

class MyHomePage extends StatefulWidget {
const MyHomePage({super.key, required this.title});

// This widget is the home page of your application. It is stateful, meaning
// that it has a State object (defined below) that contains fields that affect
// how it looks.

// This class is the configuration for the state. It holds the values (in this
// case the title) provided by the parent (in this case the App widget) and
// used by the build method of the State. Fields in a Widget subclass are
// always marked "final".
class MyHomePage extends StatelessWidget {
final String? errorMessage;

final String title;
const MyHomePage({super.key, this.errorMessage});

@override
State<MyHomePage> createState() => _MyHomePageState();
}
Widget build(BuildContext context) {
// Additional project information to display
String projectInfo = '''
Flutter Firebase Demo

class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
Firebase Connection Status: ${Firebase.apps.isNotEmpty ? 'Connected' : 'Not Connected'}

void _incrementCounter() {
setState(() {
// This call to setState tells the Flutter framework that something has
// changed in this State, which causes it to rerun the build method below
// so that the display can reflect the updated values. If we changed
// _counter without calling setState(), then the build method would not be
// called again, and so nothing would appear to happen.
_counter++;
});
}
Firebase Configuration: ${DefaultFirebaseOptions.currentPlatform}
''';

@override
Widget build(BuildContext context) {
// This method is rerun every time setState is called, for instance as done
// by the _incrementCounter method above.
//
// The Flutter framework has been optimized to make rerunning build methods
// fast, so that you can just rebuild anything that needs updating rather
// than having to individually change instances of widgets.
return Scaffold(
appBar: AppBar(
// TRY THIS: Try changing the color here to a specific color (to
// Colors.amber, perhaps?) and trigger a hot reload to see the AppBar
// change color while the other colors stay the same.
backgroundColor: Theme.of(context).colorScheme.inversePrimary,
// Here we take the value from the MyHomePage object that was created by
// the App.build method, and use it to set our appbar title.
title: Text(widget.title),
title: const Text('Flutter Demo'),
),
body: Center(
// Center is a layout widget. It takes a single child and positions it
// in the middle of the parent.
child: Column(
// Column is also a layout widget. It takes a list of children and
// arranges them vertically. By default, it sizes itself to fit its
// children horizontally, and tries to be as tall as its parent.
//
// Column has various properties to control how it sizes itself and
// how it positions its children. Here we use mainAxisAlignment to
// center the children vertically; the main axis here is the vertical
// axis because Columns are vertical (the cross axis would be
// horizontal).
//
// TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint"
// action in the IDE, or press "p" in the console), to see the
// wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'You have pushed the button this many times:',
),
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
],
),
child: errorMessage != null
? Text(
errorMessage!,
style: const TextStyle(color: Colors.red, fontSize: 18),
)
:
// success like /media/image.png
Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
const Text(
'Firebase Connected Successfully!',
style: TextStyle(color: Colors.green, fontSize: 18),
),
const SizedBox(height: 50),
Text(
projectInfo,
style: const TextStyle(color: Colors.black, fontSize: 16),
textAlign: TextAlign.center,
),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: _incrementCounter,
tooltip: 'Increment',
child: const Icon(Icons.add),
), // This trailing comma makes auto-formatting nicer for build methods.
);
}
}
2 changes: 2 additions & 0 deletions macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import FlutterMacOS
import Foundation

import firebase_core

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FLTFirebaseCorePlugin.register(with: registry.registrar(forPlugin: "FLTFirebaseCorePlugin"))
}
Binary file added media/image.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/step1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/step2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/step3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/step4.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/step5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/step6-2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/step6-3.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/step6.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/step7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
47 changes: 46 additions & 1 deletion pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.3.1"
firebase_core:
dependency: "direct main"
description:
name: firebase_core
sha256: "2438a75ad803e818ad3bd5df49137ee619c46b6fc7101f4dbc23da07305ce553"
url: "https://pub.dev"
source: hosted
version: "3.8.0"
firebase_core_platform_interface:
dependency: transitive
description:
name: firebase_core_platform_interface
sha256: e30da58198a6d4b49d5bce4e852f985c32cb10db329ebef9473db2b9f09ce810
url: "https://pub.dev"
source: hosted
version: "5.3.0"
firebase_core_web:
dependency: transitive
description:
name: firebase_core_web
sha256: f967a7138f5d2ffb1ce15950e2a382924239eaa521150a8f144af34e68b3b3e5
url: "https://pub.dev"
source: hosted
version: "2.18.1"
flutter:
dependency: "direct main"
description: flutter
Expand All @@ -75,6 +99,11 @@ packages:
description: flutter
source: sdk
version: "0.0.0"
flutter_web_plugins:
dependency: transitive
description: flutter
source: sdk
version: "0.0.0"
leak_tracker:
dependency: transitive
description:
Expand Down Expand Up @@ -139,6 +168,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.9.0"
plugin_platform_interface:
dependency: transitive
description:
name: plugin_platform_interface
sha256: "4820fbfdb9478b1ebae27888254d445073732dae3d6ea81f0b7e06d5dedc3f02"
url: "https://pub.dev"
source: hosted
version: "2.1.8"
sky_engine:
dependency: transitive
description: flutter
Expand Down Expand Up @@ -208,6 +245,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "14.2.5"
web:
dependency: transitive
description:
name: web
sha256: cd3543bd5798f6ad290ea73d210f423502e71900302dde696f8bff84bf89a1cb
url: "https://pub.dev"
source: hosted
version: "1.1.0"
sdks:
dart: ">=3.5.3 <4.0.0"
flutter: ">=3.18.0-18.0.pre.54"
flutter: ">=3.22.0"
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies:
# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^1.0.8
firebase_core:

dev_dependencies:
flutter_test:
Expand Down
3 changes: 3 additions & 0 deletions windows/flutter/generated_plugin_registrant.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include "generated_plugin_registrant.h"

#include <firebase_core/firebase_core_plugin_c_api.h>

void RegisterPlugins(flutter::PluginRegistry* registry) {
FirebaseCorePluginCApiRegisterWithRegistrar(
registry->GetRegistrarForPlugin("FirebaseCorePluginCApi"));
}
1 change: 1 addition & 0 deletions windows/flutter/generated_plugins.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#

list(APPEND FLUTTER_PLUGIN_LIST
firebase_core
)

list(APPEND FLUTTER_FFI_PLUGIN_LIST
Expand Down