3

I know I have to do Firebase.initializeApp() but I don't know where to put it, I tried some answers of others questions about the same issue but no one works.

I also tried adding WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); in void main()

Main.dart

void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
 const MyApp({Key? key}) : super(key: key);
 // This widget is the root of your application.
 @override
 Widget build(BuildContext context) {
 final AuthenticationService _authenticationService =
 AuthenticationService();
 final AuthenticationBloc _authenticationBloc =
 AuthenticationBloc(_authenticationService);
 return AuthenticationBlocProvider(
 authenticationBloc: _authenticationBloc,
 key: null,
 child: StreamBuilder(
 initialData: null,
 stream: _authenticationBloc.user,
 builder: (BuildContext context, AsyncSnapshot snapshot) {
 if (snapshot.connectionState == ConnectionState.waiting) {
 return Container(
 color: Colors.pink,
 child: CircularProgressIndicator(),
 );
 } else if (snapshot.hasData) {
 return HomeBlocProvider(
 homeBloc: HomeBloc(DbFirestoreService(),
 _authenticationService), // Inject the DbFirestoreService() & AuthenticationService()
 uid: snapshot.data,
 key: null,
 child: _buildMaterialApp(HomePage()),
 );
 } else {
 return _buildMaterialApp(Login());
 }
 },
 ),
 );
 }
 MaterialApp _buildMaterialApp(Widget homePage) {
 return MaterialApp(
 debugShowCheckedModeBanner: false,
 title: 'Journal',
 theme: ThemeData(
 primarySwatch: Colors.lightGreen,
 canvasColor: Colors.lightGreen.shade50,
 bottomAppBarColor: Colors.lightGreen,
 ),
 home: homePage,
 );
 }
}

auth.dart

class AuthenticationService implements AuthenticationApi {
 final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
 FirebaseAuth getFirebaseAuth() {
 return _firebaseAuth;
 }
 Future<String> currentUserUid() async {
 var user = _firebaseAuth.currentUser;
 return user!.uid;
 }
 Future<void> signOut() async {
 return _firebaseAuth.signOut();
 }
 Future<String> signInWithEmailAndPassword(
 {required String email, required String password}) async {
 final UserCredential user = await _firebaseAuth.signInWithEmailAndPassword(
 email: email, password: password);
 return user.user!.uid;
 }
 Future<String> createUserWithEmailAndPassword(
 {required String email, required String password}) async {
 final UserCredential user = await _firebaseAuth
 .createUserWithEmailAndPassword(email: email, password: password);
 return user.user!.uid;
 }
}

I am new with flutter. Thanks for the help

JideGuru
7,7507 gold badges29 silver badges49 bronze badges
asked Feb 2, 2022 at 13:07
3
  • 1
    What happens when you tried adding WidgetsFlutterBinding.ensureInitialized(); await Firebase.initializeApp(); in void main()? Commented Feb 2, 2022 at 13:12
  • I tried that but then the app stacks in if( snapshot.connectionState == ConnectionState.waiting Commented Feb 2, 2022 at 13:31
  • Do it like await Firebase.initializeApp( options: DefaultFirebaseOptions.currentPlatform, ); Commented Aug 16, 2022 at 18:51

4 Answers 4

4

You should initialize your Firebase like this:

void main() async {
 WidgetsFlutterBinding.ensureInitialized();
 await Firebase.initializeApp();
 runApp(MyApp());
}

Make sure to import Firebase core

import 'package:firebase_core/firebase_core.dart';
answered Feb 2, 2022 at 13:10
Sign up to request clarification or add additional context in comments.

1 Comment

I tried that but then the app stacks in if( snapshot.connectionState == ConnectionState.waiting)
4
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';
...
Future<void> main() async {
 WidgetsFlutterBinding.ensureInitialized();
 await Firebase.initializeApp({
 appId: 'your_app_id',
 apiKey: 'your_db_api_key',
 messagingSenderId: 'your_sender_id',
 projectId: 'your_project name',
 });
 runApp(MyApp());
}
...

those app_id, api_key, sender_id, project_name will be available from the firebase console easily.

answered Feb 2, 2022 at 15:46

2 Comments

now it gets stuck in snapshot.connectionState == ConnectionState.waiting . By the way, it didn't admit parameters in Firebase.initializeApp()
Are you using it for web app (or) for android/ios app
0

The initialization should happen in your main.dart while launching the app.

import 'package:firebase_core/firebase_core.dart';

This is the import to have Firebase class visible to our dart file.

void main() async {
 WidgetsFlutterBinding.ensureInitialized();
 await Firebase.initializeApp();
 runApp(MyApp());
}

This line ensures firebase is initialised properly before you could use in your app. Its an async operation so await is important too.

await Firebase.initializeApp();

dependency in pubspec.yaml

firebase_core: ^2.25.4

Dart Version:

 dart --version
Dart SDK version: 3.2.6 (stable)

Flutter Version:

flutter --version
Flutter 3.16.9 • channel stable •
answered Feb 21, 2024 at 19:01

Comments

0

Check your node version and add firebase-tools which is compatible to that version. In my case it was: 9.23.1 for node 16

  1. npm install -g [email protected]
  2. dart pub global activate flutterfire_cli
  3. flutterfire configure

This will create firebase_options.dart file.

Now, import it in the file where you are initializing the app

import 'firebase_options.dart'
 Future<void> main() async {
 WidgetsFlutterBinding.ensureInitialized();
 await Firebase.initializeApp(
 options: DefaultFirebaseOptions.currentPlatform
 );
 runApp(const MyApp());
 }

answered Mar 11, 2024 at 12:12

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.