Syed Irfan Hussaini
Syed Irfan Hussaini

Reputation: 473

FirebaseError: Firebase: No Firebase App '[DEFAULT]' has been created - but i have initialize my flutter app

I have created a new Flutter Web project and connected it with Firebase and it is showing me this error when I run it

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

I have initialize my app in main.dart file

Edit: Source Code

registrationLogic.dart - All my firebase usage is in this file and it was working before but now it is not working i have completed my sign up , sign in and forget password screen

import 'dart:io';
import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_facebook_auth/flutter_facebook_auth.dart';
import 'package:google_sign_in/google_sign_in.dart';

class RegistrationLogic {
final BuildContext context;
final FirebaseAuth _auth = FirebaseAuth.instance;
final FirebaseFirestore _firestore = FirebaseFirestore.instance;

RegistrationLogic(this.context);

void getPlatform(bool nextStep) {
  FirebaseAuth.instance.authStateChanges().listen((User user) {
    if (user == null) {
      print('User is currently signed out!');
    } else {
      print('User is signed in!');
    }
  });
  try {
    if (Platform.isIOS || Platform.isAndroid) {
      nextStep ? googleSignInMobile() : facebookSignInMobile();
    }
  } catch (e) {
  if (e.toString() == 'Unsupported operation: Platform._operatingSystem') {
    nextStep ? googleSignInMobile() : facebookSignInWeb();
    }
   }
 }

Future<void> googleSignInMobile() async {
  final GoogleSignInAccount googleUser = await GoogleSignIn().signIn();
  final GoogleSignInAuthentication googleAuth =
      await googleUser.authentication;
  final credential = GoogleAuthProvider.credential(
    accessToken: googleAuth.accessToken,
    idToken: googleAuth.idToken,
);
  await _auth.signInWithCredential(credential);
  saveUserData(
    name: googleUser.displayName,
    email: googleUser.email,
    password: 'google',
    image: googleUser.photoUrl,
  );
}

Future<void> facebookSignInMobile() async {
  final AccessToken result = await FacebookAuth.instance.login();
  final facebookAuthCredential =
  FacebookAuthProvider.credential(result.token);
  _auth.signInWithCredential(facebookAuthCredential);

  saveUserData(
    name: _auth.currentUser.displayName,
    email: _auth.currentUser.email,
    password: 'google',
    image: _auth.currentUser.photoURL,
  );
}

Future<void> googleSignInWeb() async {
  GoogleAuthProvider googleProvider = GoogleAuthProvider();

  googleProvider
      .addScope('https://www.googleapis.com/auth/contacts.readonly');
  googleProvider.setCustomParameters({
    'login_hint': '[email protected]',
  });

  await _auth.signInWithPopup(googleProvider);

  saveUserData(
    name: _auth.currentUser.displayName,
    email: _auth.currentUser.email,
    password: 'google',
    image: _auth.currentUser.photoURL,
  );
}

Future<void> facebookSignInWeb() async {
  FacebookAuthProvider facebookProvider = FacebookAuthProvider();
  facebookProvider.addScope('email');
   facebookProvider.setCustomParameters({
    'display': 'popup',
  });

  await _auth.signInWithPopup(facebookProvider);

  saveUserData(
    name: _auth.currentUser.displayName,
    email: _auth.currentUser.email,
    password: 'facebook',
    image: _auth.currentUser.photoURL,
  );
}

Future<void> signUp(String name, String email, String password) async {
  try {
    await _auth.createUserWithEmailAndPassword(
        email: email, password: password);
    saveUserData(
      name: name,
      email: email,
      password: password,
    );
  } on FirebaseAuthException catch (e) {
    showOkAlertDialog(
      context: context,
      title: e.message,
    );
  } catch (e) {
    print(e);
  }
}

Future<void> signIn(String email, String password) async {
  String error;
  try {
    await _auth.signInWithEmailAndPassword(
      email: email,
      password: password,
    );
  } on FirebaseAuthException catch (e) {
    if (e.code == 'user-not-found') {
      error = 'No user found for that email.';
    } else if (e.code == 'wrong-password') {
      error = 'Password doesn\'t match.';
    }
  } catch (e) {
    error = e.toString();
  }
  if (error != null) {
    showOkAlertDialog(
      context: context,
      title: error,
    );
  }
}

void saveUserData(
    {String name,
    String email,
    String password,
    String image = 'default'}) async {
  await _firestore.collection('users').add({
    'name': name,
    'email': email,
    'password': password,
    'profileImage': image,
  });
}

Future<void> forgetPassword(String email) async {
  String message;
  try {
    await _auth.sendPasswordResetEmail(email: email);
    message = 'Reset password Link has send to your email address.';
  } on FirebaseAuthException catch (e) {
    message = e.message;
  } catch (e) {
    message = e;
  }
  showOkAlertDialog(context: context, title: message);
}
}

Firebase Plugin Version -

  firebase_core: ^1.2.1
  firebase_auth: ^1.3.0
  cloud_firestore: ^2.2.1

Firebase Plugin In Web Vesrion -

  <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-app.js </script>
  <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-analytics.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-auth.js"></script>
  <script src="https://www.gstatic.com/firebasejs/8.6.1/firebase-firestore.js"></script>

Upvotes: 0

Views: 116

Answers (1)

sungkd123
sungkd123

Reputation: 403

Check if this works

Future<void> main()  async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();

  runApp(MaterialApp(
    debugShowCheckedModeBanner: false,
    initialRoute: '/',
    routes: {
      '/': (context) => LoadScreen(),
    },
  ));
}

Upvotes: 1

Related Questions