Marina Makarets
Marina Makarets

Reputation: 29

Flutter: No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

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

Where create call Firebase.initializeApp() ?

what should I change in my code? Is there any way to do it? In case you want to see the code please let me know I will update more.

auth.dart

import 'package:firebase_auth/firebase_auth.dart';
import 'package:quiz2/model/user.dart';

class AuthService {
  FirebaseAuth _auth = FirebaseAuth.instance;
  UserFire _userFromFirebase(User user){
    return user != null ? UserFire(uid: user.uid):null;

  }

  Future signInEmailAndPass(String email, String password) async {
    try {
      UserCredential authResult = await _auth.signInWithEmailAndPassword(
          email: email, password: password);
      User firebaseUser = authResult.user;
       return _userFromFirebase(firebaseUser);

    } catch (e) {
      print(e.toString());
    }
  }

   Future signUpWithEmailAndPassword(String email, String password) async{

    try{
      
      UserCredential authResult = await _auth.createUserWithEmailAndPassword(email: email, password: password);
      User firebaseUser = authResult.user;
      return _userFromFirebase(firebaseUser);
    }catch(e){
      print(e.toString());
    }
   }
 
 Future signOut() async{
   try{
       return await _auth.signOut();
   }catch(e){
      print(e.toString());
      return null;
   }
 }
}

signup.dart

import 'package:flutter/material.dart';
import 'package:quiz2/database/auth.dart';
import 'package:quiz2/screens/landing.dart';
import 'package:quiz2/screens/signin.dart';
import 'package:quiz2/widgets/widget.dart';

class SignUp extends StatefulWidget {
  @override
  _SignUpState createState() => _SignUpState();
}

class _SignUpState extends State<SignUp> {
  final _formKey = GlobalKey<FormState>();
  String email, password, name;
  AuthService authService = new AuthService();

  bool _isLoading = false;

  signUp() async {
    if (_formKey.currentState.validate()) {
      setState(() {
        _isLoading = true;
      });

       authService.signUpWithEmailAndPassword(email, password).then((val) {
        if (val != null) {
          setState(() {
            _isLoading = false;
          });

          Navigator.pushReplacement(
              context, MaterialPageRoute(builder: (context) => MyHomePage()));
        }
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: appBar(context),
        backgroundColor: Colors.transparent,
        elevation: 0.0,
        brightness: Brightness.light,
      ),
      body: _isLoading
          ? Container(child: Center(child: CircularProgressIndicator()))
          : Form(
        key: _formKey,
        child: Container(
            padding: EdgeInsets.symmetric(horizontal: 24),
            child: Column(
              children: [
                Spacer(),
                TextFormField(
                  validator: (val) {
                    return val.isEmpty ? 'Enter Name' : null;
                  },
                  decoration: InputDecoration(hintText: 'Name'),
                  onChanged: (val) {
                    name = val;
                  },
                ),
                SizedBox(
                  height: 6,
                ),
                TextFormField(
                  validator: (val) {
                    return val.isEmpty ? 'Enter Email' : null;
                  },
                  decoration: InputDecoration(hintText: 'Email'),
                  onChanged: (val) {
                    email = val;
                  },
                ),
                SizedBox(
                  height: 6,
                ),
                TextFormField(
                  obscureText: true,
                  validator: (val) {
                    return val.isEmpty ? 'Enter password' : null;
                  },
                  decoration: InputDecoration(hintText: 'Password'),
                  onChanged: (val) {
                    password = val;
                  },
                ),
                SizedBox(
                  height: 24,
                ),
                GestureDetector(
                  onTap: () {
                    signUp();
                  },
                  child: Container(
                    padding: EdgeInsets.symmetric(vertical: 16),
                    decoration: BoxDecoration(
                        color: Colors.teal,
                        borderRadius: BorderRadius.circular(30)),
                    alignment: Alignment.center,
                    width: MediaQuery.of(context).size.width - 48,
                    child: Text(
                      "Sign Up",
                      style: TextStyle(color: Colors.white, fontSize: 20),
                    ),
                  ),
                ),
                SizedBox(
                  height: 18,
                ),
                Row(
                  mainAxisAlignment: MainAxisAlignment.center,
                  children: [
                    Text(
                      "Alredy have an account?",
                      style: TextStyle(fontSize: 16),
                    ),
                    GestureDetector(
                        onTap: () {
                          Navigator.pushReplacement(
                              context,
                              MaterialPageRoute(
                                  builder: (context) => SignIn()));
                        },
                        child: Text(" Sign In",
                            style: TextStyle(
                                fontSize: 16,
                                decoration: TextDecoration.underline)))
                  ],
                ),
                SizedBox(
                  height: 80,
                ),
              ],
            )),
      ),
    );
  }
}

user.dart

class UserFire{
  String uid;
  UserFire({this.uid});
}

what should I change in my code? Is there any way to do it? In case you want to see the code please let me know I will update more.

Upvotes: 0

Views: 205

Answers (2)

Nabin Dhakal
Nabin Dhakal

Reputation: 2202

You need initialize your firebase .Do as follows:

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

Upvotes: 1

DJafari
DJafari

Reputation: 13545

Did you complete all the installation steps?

if you answer is yes :

you must check this link

Upvotes: 0

Related Questions