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.
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
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
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:
In conclusion, this will initialize Firebase before running your application.