0

[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.

0

2 Answers 2

1

You need initialize your firebase .Do as follows:

void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      await Firebase.initializeApp();
      runApp(MyApp());
    }
Sign up to request clarification or add additional context in comments.

2 Comments

but I have this code in main.dart void main() { runApp(ProviderScope(child: MyApp())); }
Add WidgetsFlutterBinding.ensureInitialized(); and await Firebase.initializeApp(); in your main function. Let me know if this works or not
0

Did you complete all the installation steps?

if you answer is yes :

you must check this link

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.