Home > Net >  Trying to connect Firebase to Flutter App
Trying to connect Firebase to Flutter App

Time:07-13

I am trying to connect my firebase to my flutter application but it is not working. I followed the documentation (Error thrown on the emulator

main.dart

import 'package:flutter/material.dart';
import 'package:barbellplus/routes.dart';

// Import the firebase_core plugin
import 'package:firebase_core/firebase_core.dart';

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const App());
}

/// We are using a StatefulWidget such that we only create the [Future] once,
/// no matter how many times our widget rebuild.
/// If we used a [StatelessWidget], in the event where [App] is rebuilt, that
/// would re-initialize FlutterFire and make our application re-enter loading state,
/// which is undesired.
class App extends StatefulWidget {
  const App({super.key});

  @override
  State<App> createState() => _AppState();
}

class _AppState extends State<App> {
  /// The future is part of the state of our widget. We should not call `initializeApp`
  /// directly inside [build].
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
      // Initialize FlutterFire:
      future: _initialization,
      builder: (context, snapshot) {
        // Check for errors
        if (snapshot.hasError) {
          return const Text('error', textDirection: TextDirection.ltr);
        }

        // Once complete, show your application
        if (snapshot.connectionState == ConnectionState.done) {
          return MaterialApp(
            routes: appRoutes,
          );
        }

        // Otherwise, show something whilst waiting for initialization to complete
        return const Text('loading', textDirection: TextDirection.ltr);
      },
    );
  }
}

If anyone can help me it would be much appreciated.

CodePudding user response:

I am not quite sure what the problem is since I can't see the error log but here is a quite good explanation of connecting Firebase to Flutter by the official Firebase Youtube channel: https://www.youtube.com/watch?v=EXp0gq9kGxI

CodePudding user response:

You should follow this documentation instead -> Click me!

This will automatically generate a firebase_options.dart in your lib folder.

Then, your main.dart should look like this:

import 'package:bidding/firebase_options.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
    
Future<void> main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform,
      );
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
      const MyApp({Key? key}) : super(key: key);
    
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          debugShowCheckedModeBanner: false,
          home: MyHomePage()
        );
      }
    }

In conclusion, this will initialize Firebase before running your application.

  • Related