0

I'm creating a Flutter App and in my main.dart I wanna check if is a user logged in and if it is to display main menu screen otherwise the welcome screen, but when I run the app it throws me

[core/no-app] No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

But I did initialize it, can you please look over my code, maybe I haven't called all the methods correctly

import 'package:flutter/material.dart';
import 'package:modal_progress_hud/modal_progress_hud.dart';
import 'package:status_alert/status_alert.dart';
import 'package:vhunt/screens/login_screen.dart';
import 'package:vhunt/screens/main_menu.dart';
import 'package:vhunt/screens/register_screen.dart';
import 'package:vhunt/screens/welcome_screen.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_auth/firebase_auth.dart';

User loggedInUser;
void main() {
  runApp(myApp());
}

class myApp extends StatefulWidget {
  @override
  _myAppState createState() => _myAppState();
}

class _myAppState extends State<myApp> {
  final _auth = FirebaseAuth.instance;
  bool _initialized = false;
  bool _error = false;
  void initializeFlutterFire() async {
    try {
      await Firebase.initializeApp();
      setState(() {
        _initialized = true;
      });
    } catch (e) {
      setState(() {
        _error = true;
      });
    }
  }

  void checkUserLogged() async {
    try {
      if (_initialized == true) {
        final user = await _auth.currentUser;
        if (user != null) {
          loggedInUser = user;
        }
      }
    } catch (e) {
      print(e);
    }
  }

  @override
  void initState() {
    initializeFlutterFire();
    checkUserLogged();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    if (_error) {
      StatusAlert.show(context,
          duration: Duration(seconds: 3),
          title: 'Something Went Wrong',
          subtitle: 'We are sorry, try again later',
          configuration: IconConfiguration(
            icon: Icons.close,
          ));
    }

    return MaterialApp(
      initialRoute: loggedInUser == null ? WelcomeScreen.id : MainMenu.id,
      routes: {
        WelcomeScreen.id: (context) => WelcomeScreen(),
        RegistrationScreen.id: (context) => RegistrationScreen(),
        LogInScreen.id: (context) => LogInScreen(),
        MainMenu.id: (context) => MainMenu(
              loggedinUser: loggedInUser,
            ),
      },
    );
  }
}

Before I added the Firebase Auth it worked fine. Thank you in advance!

  • 3
    Does this answer your question? [Error: No Firebase App '\[DEFAULT\]' has been created - call Firebase App.initializeApp()](https://stackoverflow.com/questions/40563140/error-no-firebase-app-default-has-been-created-call-firebase-app-initiali) – Merym Sep 09 '20 at 15:17
  • I didn't know I have to call it before I initialize the app –  Sep 09 '20 at 15:19

1 Answers1

0

You have to call Firebase.initializeApp() before FirebaseAuth.instance, and any other use if the Firebase SDK.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441