0

I want to access from Sign In to Home window, but when I try to sign in with existing account the button doesn't work and the window stays the same.

import 'package:finalproject/Pages/home.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';

class LoginPage extends StatefulWidget {
  @override
  _LoginPageState createState() => new _LoginPageState();
}

class _LoginPageState extends State<LoginPage> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();
  String _email, _password; 

 @override
 Widget build(BuildContext context) {
return new Scaffold(
  appBar: new AppBar(
    title: Text('Sign in'),
  ),
  body: Form(
    key: _formKey, 
    child: Column(
      children: <Widget>[

        TextFormField(
          validator: (input) {
            if (input.isEmpty) {
              return '  Write your email';
            }
            return null;
          },
          onSaved: (input) => _email = input,
          decoration: InputDecoration(labelText: '  Email'),
        ),

        TextFormField(
          validator: (input) {
            if (input.length < 6) {
              return '  Write your password';
            }
            return null;
          },
          onSaved: (input) => _password = input,
          decoration: InputDecoration(labelText: '  Password'),
          obscureText: true,
        ),

        RaisedButton(
          onPressed: signIn,
          child: Text('Sign in'),
        ),],),),);}


void signIn() async {

if (_formKey.currentState.validate()) {
  _formKey.currentState.save();
  try {
    // ignore: deprecated_member_use
    UserCredential authResult = await FirebaseAuth.instance
        .signInWithEmailAndPassword(email: _email, password: _password);
    final User user = authResult.user;

    Navigator.push(
      context,
      MaterialPageRoute(builder: (context) => Home()),
    );
  } catch (e) {
    print(e.message);
  }}}}

Also, I always get this message after I press the Sign in button.

I/flutter ( 3130): No Firebase App '[DEFAULT]' has been created - call Firebase.initializeApp()

Maybe this could be the reason of it?

Chorche
  • 85
  • 3
  • 9
  • Please search for an error message before posting a new question, as this has been covered quite well before: https://stackoverflow.com/search?q=%5Bflutter%5D++Firebase.initializeApp%28%29 – Frank van Puffelen Jan 01 '21 at 20:39

1 Answers1

1

You need to initialize the FirebaseApp. You can do it in your main function like this:

void main() async {
 
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp();
   
  runApp(MyApp());
}
osaxma
  • 2,657
  • 2
  • 12
  • 21