skip to Main Content

I am trying to connect my firebase to my flutter application but it is not working. I followed the documentation (https://firebase.flutter.dev/docs/manual-installation/android), but I am having problems with it. Every time it fails to connect and throws an error.

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);
      },
    );
  }
}

Error

E/flutter ( 9082): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: PlatformException(channel-error, Unable to establish connection on channel., null, null)
E/flutter ( 9082): #0      FirebaseCoreHostApi.initializeCore (package:firebase_core_platform_interface/src/pigeon/messages.pigeon.dart:205:7)
E/flutter ( 9082): <asynchronous suspension>
E/flutter ( 9082): #1      MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:29:44)
E/flutter ( 9082): <asynchronous suspension>
E/flutter ( 9082): #2      MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:73:7)
E/flutter ( 9082): <asynchronous suspension>
E/flutter ( 9082): #3      Firebase.initializeApp (package:firebase_core/src/firebase.dart:40:31)
E/flutter ( 9082): <asynchronous suspension>
E/flutter ( 9082): #4      main (package:barbellplus/main.dart:8:3)
E/flutter ( 9082): <asynchronous suspension>
E/flutter ( 9082): 

If anyone can help me it would be much appreciated.

2

Answers


  1. 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

    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search