Add Firebase

We will add Firebase to the code, Firebase is a BaaS (Backend-as-a-Service) offering a wide range of features that would be very complex to implement on your own.

Adding Firebase to a mobile or web app used to be a long and painful process.

Firebase CLI https://firebase.google.com/docs/flutter/setup?platform=android

npm install -g firebase-tools
firebase login
dart pub global activate flutterfire_cli
export PATH="$PATH":"$HOME/.pub-cache/bin"

Use flutterfire CLI to either create a new Firebase project or use an existing

flutterfire configure
flutterfire configure --project=todo-flutter-8a80f

This will create a new file -> firebase_options.dart and other files

lib/firebase_options.dart
ios/Runner/GoogleService-Info.plist
ios/firebase_app_id_file.json
macos/Runner/GoogleService-Info.plist
macos/firebase_app_id_file.json
android/app/google-services.json

Update pubspec.yaml with

firebase_core: ^2.25.4

We need to initialise firebase now

Modify MyApp class -> build method

Widget build(BuildContext context) {
    return FutureBuilder(
      future: Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      ),
      builder: (context, snapshot) {
        if (snapshot.connectionState == ConnectionState.done) {
          return const MaterialApp(
            debugShowCheckedModeBanner: false,
            home: 1 == 1 ? LoginScreen() : HomeScreen(),
          );
        } else {
          return MaterialApp(
            debugShowCheckedModeBanner: false,
            home: Scaffold(
              body: Container(),
            ),
          );
        }
      },
    );
  }

Run the App, Restart

Last updated