0

Good morning everyone,

I wanted to add firebase to my flutter project, I followed all the instructions and ended up on this page : https://firebase.flutter.dev/docs/overview#initializing-flutterfire

So far, i have been wanting to just check if my user is logged in, display one screen if he is, and another one if he's not logged in.

Please check the whole code :

import 'package:flutter/material.dart';

import 'package:firebase_auth/firebase_auth.dart';
import 'package:firebase_core/firebase_core.dart';

import './controller/main_app_controller.dart';
import './controller/log_controller.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  final Future<FirebaseApp> _initialization = Firebase.initializeApp();

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: _initialization,
        builder: (context, snapshot) {
          if (snapshot.connectionState == ConnectionState.waiting ||
              snapshot.hasError) {
            print('error');
          }
          if (snapshot.connectionState == ConnectionState.done) {
            return MaterialApp(
              title: 'Flutter Demo',
              debugShowCheckedModeBanner: false,
              theme: ThemeData(
                primarySwatch: Colors.blue,
                visualDensity: VisualDensity.adaptivePlatformDensity,
              ),
              home: StreamBuilder(
                  stream: FirebaseAuth.instance.authStateChanges(),
                  builder: (context, userSnapshot) {
                    if (userSnapshot.hasData) {
                      return MainAppController();
                    }
                    return LogController();
                  }),
            );
          }
        });
  }
}

I get the error : [core/no-app] No firebase App '[default]' has been created - Call Firebase.initializeApp()

I don't understand because I am actually calling intializeApp in the future builder.

Does someone has an idea of what i am doing wrong ?

Thank you

Ps : find below my pubspec.yaml

dev_dependencies:
  flutter_test:
    sdk: flutter
  firebase_auth: ^0.18.4+1
  firebase_core: ^0.5.3
  cloud_firestore: ^0.14.4
  firebase_storage: ^5.2.0
Mikev60
  • 103
  • 1
  • 8
  • Have you tried call in main method, before the runApp? – bcihan Jan 06 '21 at 08:09
  • Thanks for answering. I did, but it's not working... – Mikev60 Jan 06 '21 at 08:13
  • I want to be sure, your main method should be like this. https://stackoverflow.com/a/63537567/2105320 – bcihan Jan 06 '21 at 08:23
  • Well it looks to be working with your code now, but i don't understand what was wrong in my previous code ? – Mikev60 Jan 06 '21 at 08:27
  • I dont't know either :/ – bcihan Jan 06 '21 at 08:32
  • You're missing an `await` on `final Future _initialization = Firebase.initializeApp();`. If you change that to `final FirebaseApp _initialization = await Firebase.initializeApp();` it should work. But I recommend moving it to the `main()` method as the answer @bcihan linked shows. – Frank van Puffelen Jan 06 '21 at 14:41
  • In this case, you don't need to wait for the Future. Otherwise why we are using FutureBuilder, am i wrong? – bcihan Jan 06 '21 at 15:07

0 Answers0